private MimeMessage CreateMailMessage(MailMessage notification) { var message = new MimeMessage { Subject = notification.Subject }; var builder = new BodyBuilder(); if (!String.IsNullOrEmpty(notification.To)) { message.To.AddRange(InternetAddressList.Parse(notification.To)); } if (!String.IsNullOrEmpty(notification.From)) { message.From.AddRange(InternetAddressList.Parse(notification.From)); } else { message.From.AddRange(InternetAddressList.Parse(_emailOptions.SmtpFrom)); } if (!String.IsNullOrEmpty(notification.Body)) { builder.HtmlBody = notification.Body; } message.Body = builder.ToMessageBody(); return(message); }
public Task SendAsync(MailMessage model) { _recentMessages.Enqueue(model); Interlocked.Increment(ref _totalSent); while (_recentMessages.Count > _messagesToStore) _recentMessages.Dequeue(); return Task.CompletedTask; }
public Task SendAsync(MailMessage model) { _recentMessages.Enqueue(model); Interlocked.Increment(ref _totalSent); _waitHandle.Set(); while (_recentMessages.Count > _messagesToStore) _recentMessages.Dequeue(); return Task.FromResult(0); }
public async Task SendAsync(MailMessage model) { _logger.LogTrace("Creating Mail Message from model"); var message = CreateMailMessage(model); message.Headers.Add("X-Mailer-Machine", Environment.MachineName); message.Headers.Add("X-Mailer-Date", SystemClock.UtcNow.ToString()); message.Headers.Add("X-Auto-Response-Suppress", "All"); message.Headers.Add("Auto-Submitted", "auto-generated"); using (var client = new SmtpClient(new ExtensionsProtocolLogger(_logger))) { string host = _emailOptions.SmtpHost; int port = _emailOptions.SmtpPort; var encryption = GetSecureSocketOption(_emailOptions.SmtpEncryption); _logger.LogTrace("Connecting to SMTP server: {SmtpHost}:{SmtpPort} using {Encryption}", host, port, encryption); var sw = Stopwatch.StartNew(); await client.ConnectAsync(host, port, encryption).AnyContext(); _logger.LogTrace("Connected to SMTP server took {Duration:g}", sw.Elapsed); // Note: since we don't have an OAuth2 token, disable the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); string user = _emailOptions.SmtpUser; if (!String.IsNullOrEmpty(user)) { _logger.LogTrace("Authenticating {SmtpUser} to SMTP server", user); sw.Restart(); await client.AuthenticateAsync(user, _emailOptions.SmtpPassword).AnyContext(); _logger.LogTrace("Authenticated to SMTP server took {Duration:g}", user, sw.Elapsed); } _logger.LogTrace("Sending message: to={To} subject={Subject}", message.Subject, message.To); sw.Restart(); await client.SendAsync(message).AnyContext(); _logger.LogTrace("Sent Message took {Duration:g}", sw.Elapsed); sw.Restart(); await client.DisconnectAsync(true).AnyContext(); _logger.LogTrace("Disconnected from SMTP server took {Duration:g}", sw.Elapsed); sw.Stop(); } _lastSuccessfulConnection = SystemClock.UtcNow; }