private static MimeMessage getEmailMessage( string toName, string toAddress, string subject, string message, IEnumerable <string> attachmentFiles, SmtpConfig smtpConfig, MailHeaders headers) { var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress(smtpConfig.FromName, smtpConfig.FromAddress)); emailMessage.Subject = subject.ApplyRle(); emailMessage.To.Add(new MailboxAddress(toName ?? string.Empty, toAddress)); emailMessage.Body = getMessageBody(message, attachmentFiles); addHeaders(emailMessage, headers, smtpConfig.FromAddress); return(emailMessage); }
/// <summary> /// Sends an email using the `MailKit` library. /// This method converts a razor template file to an string and then uses it as the email's message. /// </summary> public async Task SendEmailAsync <T>( SmtpConfig smtpConfig, IEnumerable <MailAddress> emails, string subject, string viewNameOrPath, T viewModel, DelayDelivery delayDelivery = null, IEnumerable <string> attachmentFiles = null, MailHeaders headers = null) { var message = await _viewRendererService.RenderViewToStringAsync(viewNameOrPath, viewModel); await SendEmailAsync(smtpConfig, emails, subject, message, delayDelivery, attachmentFiles, headers); }
private static MimeMessage getEmailMessage( string toName, string toAddress, string subject, string message, IEnumerable <string> attachmentFiles, SmtpConfig smtpConfig, MailHeaders headers, IEnumerable <MailAddress> blindCarpbonCopies, IEnumerable <MailAddress> carpbonCopies, IEnumerable <MailAddress> replyTos) { var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress(smtpConfig.FromName, smtpConfig.FromAddress)); emailMessage.Subject = subject.ApplyRle(); emailMessage.To.Add(new MailboxAddress(toName ?? string.Empty, toAddress)); if (blindCarpbonCopies != null && blindCarpbonCopies.Any()) { foreach (var bcc in blindCarpbonCopies) { emailMessage.Bcc.Add(new MailboxAddress(bcc.ToName ?? string.Empty, bcc.ToAddress)); } } if (carpbonCopies != null && carpbonCopies.Any()) { foreach (var cc in carpbonCopies) { emailMessage.Cc.Add(new MailboxAddress(cc.ToName ?? string.Empty, cc.ToAddress)); } } if (replyTos != null && replyTos.Any()) { foreach (var rt in replyTos) { emailMessage.ReplyTo.Add(new MailboxAddress(rt.ToName ?? string.Empty, rt.ToAddress)); } } emailMessage.Body = getMessageBody(message, attachmentFiles); addHeaders(emailMessage, headers, smtpConfig.FromAddress); return(emailMessage); }
private static void addHeaders(MimeMessage emailMessage, MailHeaders headers, string fromAddress) { if (headers == null) { return; } var host = fromAddress.Split('@').Last(); if (!string.IsNullOrWhiteSpace(headers.MessageId)) { emailMessage.MessageId = $"<{headers.MessageId}@{host}>"; } if (!string.IsNullOrWhiteSpace(headers.InReplyTo)) { emailMessage.InReplyTo = $"<{headers.InReplyTo}@{host}>"; } if (!string.IsNullOrWhiteSpace(headers.References)) { emailMessage.References.Add($"<{headers.References}@{host}>"); } }
/// <summary> /// Sends an email using the `MailKit` library. /// </summary> public async Task SendEmailAsync( SmtpConfig smtpConfig, IEnumerable <MailAddress> emails, string subject, string message, IEnumerable <MailAddress> blindCarpbonCopies = null, IEnumerable <MailAddress> carpbonCopies = null, IEnumerable <MailAddress> replyTos = null, DelayDelivery delayDelivery = null, IEnumerable <string> attachmentFiles = null, MailHeaders headers = null) { if (smtpConfig.UsePickupFolder) { const int maxBufferSize = 0x10000; // 64K. foreach (var email in emails) { using (var stream = new FileStream( Path.Combine(smtpConfig.PickupFolder, $"email-{Guid.NewGuid().ToString("N")}.eml"), FileMode.CreateNew, FileAccess.Write, FileShare.None, maxBufferSize, useAsync: true)) { var emailMessage = getEmailMessage(email.ToName, email.ToAddress, subject, message, attachmentFiles, smtpConfig, headers, blindCarpbonCopies, carpbonCopies, replyTos); await emailMessage.WriteToAsync(stream); } } } else { using (var client = new SmtpClient()) { if (!string.IsNullOrWhiteSpace(smtpConfig.LocalDomain)) { client.LocalDomain = smtpConfig.LocalDomain; } await client.ConnectAsync(smtpConfig.Server, smtpConfig.Port, SecureSocketOptions.Auto); if (!string.IsNullOrWhiteSpace(smtpConfig.Username) && !string.IsNullOrWhiteSpace(smtpConfig.Password)) { await client.AuthenticateAsync(smtpConfig.Username, smtpConfig.Password); } var count = 0; foreach (var email in emails) { var emailMessage = getEmailMessage(email.ToName, email.ToAddress, subject, message, attachmentFiles, smtpConfig, headers, blindCarpbonCopies, carpbonCopies, replyTos); await client.SendAsync(emailMessage); count++; if (delayDelivery != null) { if (count % delayDelivery.NumberOfMessages == 0) { await Task.Delay(delayDelivery.Delay); } } } await client.DisconnectAsync(true); } } }