I used SendGrid earlier to send emails on my ASP.net Core project and it was very fast but costly due to heavy load. I currently have an Office 365 account and I can use the SMTP server so I decided to use MailKit. The email gets sent successfully but it takes a very long time although I'm awaiting the call... The following is the code I use:
public async Task SendEmailAsync(string email, string subject, string message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("name", "name@domain.com"));
emailMessage.To.Add(new MailboxAddress(email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart("Html") { Text = message };
using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.office365.com", 587, SecureSocketOptions.Auto).ConfigureAwait(false);
await client.AuthenticateAsync("*******", "******");
await client.SendAsync(emailMessage).ConfigureAwait(false);
await client.DisconnectAsync(true).ConfigureAwait(false);
}
}
I have seen the suggestion in this question: MailKit SMTP slow when sending messages and I applied it but the problem still happens.
Thank you