I have such implementation of sending emails with SendGrid. That works fine, but how I can check if an email was sent correctly, of course, I'm not thinking about cases that email address was not correct one but cases like that SendGrid servers were not available or generally, something obviously wrong during email sending
public class EmailSender : IEmailSender
{
public string SendGridKey;
public string SendGridEmail;
public string SendGridName;
public EmailSender()
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
SendGridKey = configuration["EmailCredentials:SendGridKey"];
SendGridEmail = configuration["EmailCredentials:SendGridEmail"];
SendGridName = configuration["EmailCredentials:SendGridName"];
}
public Task SendEmailAsync(string email, string subject, string message)
{
return Execute(SendGridKey, subject, message, email);
}
public Task Execute(string apiKey, string subject, string message, string email)
{
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress(SendGridEmail, SendGridName),
Subject = subject,
PlainTextContent = message,
HtmlContent = message
};
msg.AddTo(new EmailAddress(email));
msg.SetClickTracking(false, false);
return client.SendEmailAsync(msg);
}
}