IMailService
[删除(380066935@qq.com或微信通知)]
更好的阅读体验请查看原文:https://codewithmukesh.com/blog/send-emails-from-aspnet-core-using-amazon-ses/
public interface IMailService
{
Task SendEmailAsync(MailRequest mailRequest);
}
public class MailService : IMailService
{
private readonly MailSettings _mailSettings;
public MailService(IOptions<MailSettings> mailSettings)
{
_mailSettings = mailSettings.Value;
}
public async Task SendEmailAsync(MailRequest mailRequest)
{
var email = new MimeMessage();
email.From.Add(new MailboxAddress(_mailSettings.DisplayName, _mailSettings.Mail));
email.To.Add(MailboxAddress.Parse(mailRequest.ToEmail));
email.Subject = mailRequest.Subject;
var builder = new BodyBuilder();
builder.HtmlBody = mailRequest.Body;
email.Body = builder.ToMessageBody();
using var smtp = new SmtpClient();
smtp.Connect(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls);
smtp.Authenticate(_mailSettings.Username, _mailSettings.Password);
await smtp.SendAsync(email);
smtp.Disconnect(true);
}
}