目录
一、EmailSender : IEmailSender
二、EmailSender : IEmailSender
三、EmailSender : IEmailSender
四、EmailSender : IEmailSender
一、EmailSender : IEmailSender
namespace WebPWrecover.Services
{
public class EmailSender : IEmailSender
{
public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor, ILogger<EmailSender> logger)
{
Options = optionsAccessor.Value;
_logger = logger;
}
二、EmailSender : IEmailSender
public AuthMessageSenderOptions Options { get; }
private readonly ILogger _logger;
public async Task SendEmailAsync(string toEmail, string subject, string message)
{
if (string.IsNullOrEmpty(Options.SendGridKey))
{
throw new Exception("Null SendGridKey");
}
await Execute(Options.SendGridKey, subject, message, toEmail);
}
三、EmailSender : IEmailSender
public async Task Execute(string apiKey, string subject, string message, string toEmail)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("jsolanoe@ucenfotec.ac.cr", "Autentificación de cuenta"),
Subject = subject,
PlainTextContent = message,
HtmlContent = message
};
四、EmailSender : IEmailSender
msg.AddTo(new EmailAddress(toEmail));
msg.SetClickTracking(false, false);
var response = await client.SendEmailAsync(msg);
_logger.LogInformation(response.IsSuccessStatusCode
? $"Email to {toEmail} queued successfully!"
: $"Failure Email to {toEmail}");
}
}
}