Created
August 1, 2023 08:24
-
-
Save prince272/2dfeff0738be4b850cf8a4b4cc34d5ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailAccount | |
{ | |
public string Username { get; set; } = default!; | |
public string Password { get; set; } = default!; | |
public string Email { get; set; } = default!; | |
public string DisplayName { get; set; } = default!; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailAttachment | |
{ | |
public string FileName { get; set; } = default!; | |
public Stream Content { get; set; } = default!; | |
public string ContentType { get; set; } = default!; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailMessage | |
{ | |
public string Subject { get; set; } = default!; | |
public string Body { get; set; } = default!; | |
public IEnumerable<EmailAttachment> Attachments { get; set; } = Array.Empty<EmailAttachment>(); | |
public IEnumerable<string> Recipients { get; set; } = Array.Empty<string>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IEmailSender | |
{ | |
Task SendEmailAsync(EmailAccount account, EmailMessage message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailKitEmailSender : IEmailSender | |
{ | |
private readonly IOptions<MailKitEmailOptions> _emailOptions; | |
public MailKitEmailSender(IOptions<MailKitEmailOptions> emailOptions) | |
{ | |
_emailOptions = emailOptions ?? throw new ArgumentNullException(nameof(emailOptions)); | |
} | |
public async Task SendEmailAsync(EmailAccount account, EmailMessage message) | |
{ | |
var minme = new MimeMessage(); | |
minme.From.Add(new MailboxAddress(account.DisplayName, account.Email)); | |
foreach (var recipient in message.Recipients) | |
{ | |
minme.To.Add(new MailboxAddress(string.Empty, recipient)); | |
} | |
minme.Subject = message.Subject; | |
var builder = new BodyBuilder(); | |
builder.HtmlBody = message.Body; | |
foreach (var attachmentInfo in message.Attachments) | |
{ | |
builder.Attachments.Add(attachmentInfo.FileName, attachmentInfo.Content, ContentType.Parse(attachmentInfo.ContentType)); | |
} | |
minme.Body = builder.ToMessageBody(); | |
using var client = new SmtpClient(); | |
if (_emailOptions.Value.EnableSsl) | |
{ | |
await client.ConnectAsync(_emailOptions.Value.SmtpHost, _emailOptions.Value.SmtpPort, SecureSocketOptions.SslOnConnect); | |
} | |
else | |
{ | |
await client.ConnectAsync(_emailOptions.Value.SmtpHost, _emailOptions.Value.SmtpPort, SecureSocketOptions.StartTls); | |
} | |
await client.AuthenticateAsync(account.Username, account.Password); | |
await client.SendAsync(minme); | |
await client.DisconnectAsync(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment