예제 #1
0
        public async void SendAsync(string to)
        {
            try
            {
                if (string.IsNullOrEmpty(From))
                {
                    throw new Exception("'From' cannot be empty");
                }
                if (string.IsNullOrEmpty(to))
                {
                    throw new ArgumentException(nameof(to));
                }
                var code    = _verificationCode.Generate(out var result);
                var text    = _parser.Parse(code);
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(From));
                message.To.Add(new MailboxAddress(to));
                message.Body = new TextPart {
                    Text = text
                };
                using (var emailClient = new SmtpClient())
                {
                    await emailClient.ConnectAsync(_configuration.SmtpServer, _configuration.SmtpPort, _configuration.UseSsl);

                    if (AuthenticateHandlerDelegate != null)
                    {
                        AuthenticateHandlerDelegate.Invoke(emailClient);
                    }
                    else
                    {
                        if (_configuration.RemoveXOauth2)
                        {
                            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
                        }
                        emailClient.Authenticate(_configuration.SmtpUsername, _configuration.SmtpPassword);
                    }

                    await emailClient.SendAsync(message);

                    MessageSentHandler?.Invoke(result, message);
                    emailClient.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
            }
        }
예제 #2
0
        public async void SendAsync <T>(Func <string, T> options) where T : class
        {
            try
            {
                if (options == null)
                {
                    throw new ArgumentException(nameof(options));
                }
                var code  = _verificationCode.Generate(out var result);
                var param = options.Invoke(code);
                if (!(param is MimeMessage))
                {
                    return;
                }
                var message = param as MimeMessage;
                using (var emailClient = new SmtpClient())
                {
                    await emailClient.ConnectAsync(_configuration.SmtpServer, _configuration.SmtpPort,
                                                   _configuration.UseSsl);

                    if (AuthenticateHandlerDelegate != null)
                    {
                        AuthenticateHandlerDelegate.Invoke(emailClient);
                    }
                    else
                    {
                        if (_configuration.RemoveXOauth2)
                        {
                            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
                        }
                        emailClient.Authenticate(_configuration.SmtpUsername, _configuration.SmtpPassword);
                    }

                    await emailClient.SendAsync(message);

                    MessageSentHandler?.Invoke(result, message);
                    emailClient.Disconnect(true);
                }
            }
            catch (Exception ex)
            {
                VerificationCodoeExceptionHandler?.Invoke(ex);
            }
        }