I am using MimeKit on aspnet core and everything is working locally but I get "Authentication failed" when I try to send email on a azure App Service.
I had to enable "Allow less secure apps" in order to get it to work locally.
This is the code I am using:
using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync(_emailOptions.Username, _emailOptions.Password);
await client.SendAsync(emailMessage);
await client.DisconnectAsync(true);
}
I have verified that _emailOptions.Username and _emailOptions.Password are correct by logging them in the exception thrown
catch (Exception ex)
{
_log.LogError(new EventId(1), ex, "Username: '{0}', Password: '{1}'", _emailOptions.Username, _emailOptions.Password);
throw;
}
Do you have any ideas of how to troubleshoot this?
I create a sample to send email using MimeKit and MailKit email framework, I find the code works fine on local and azure, if I provide valid username&password and allow less secure apps to access gmail account (the account not use 2-Step Verification).
Please make sure if the account that you provide use 2-Step Verification or not enable less secure apps to access account. Besides, SendGrid is a cloud-based email service, you could try it.