I'm having trouble with sending an email using the Gmail SMTP Server in C#. There is plenty of subjects about this matter in StackOverflow, but none of the solutions around there are working with my specifities.
My specificities are :
- App Specific Password generated and very strong password originally
- Double Auth (2 Step Verification) activated and required because of some other app
- "Allow Less Secure Apps" parameter is unavailable from Google's UI
Can someone propose a solution ? The solution "Disable 2 Step Verification" is not a valid solution are we are looking for a solution that respects main account integrity.
Maybe there is a solution using Google's API or any other config parameter ? I doubt there is a C# oriented solution as 2 Step Verification is intended to provide a more secure account, but isn't the "App Specific Password" a way to bypass the 2 Step Verification for some app ?
I have generated a specific app password : https://security.google.com/settings/security/apppasswords
Less Secure Apps parameter is not available : https://www.google.com/settings/security/lesssecureapps
Here is my code :
public string sendEmailUsingGmail(string email)
{
using (MailMessage mail = new MailMessage("from_account@gmail.com", email)
{
Subject = "Aquaponey today",
Body = "Hello, you are a Unicorn and you will be late for aquaponey today. - God",
IsBodyHtml = true,
Priority = System.Net.Mail.MailPriority.High
})
{
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "my_account@gmail.com",
Password = @"my_specific_app_password"
};
smtpClient.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtpClient.Send(mail);
return "OK";
}
}
}
The error returned by my client is :
System.Net.Mail.SmtpException: 5.5.1 Authentication Required. Learn more at
If it's not possible, may be related to How does google recognize a "trusted device" with 2-step verification
I've tried to work with the Google API for mail sending before. My understanding is that you have to establish a login (Which will trigger the two step) which provides you with an open session. And then with that open session you can send the mail. Otherwise most likely the code is timing out while waiting for the second half of the login.
Update: You can use the Server Side Authentication process, this will require a one time sign in from the end user (Which will have to go through the sign in with two factor authentication) and that will provide you with a refresh token that can use to re-connect to the server. The information is located at the Google Developers Docs
Excerpt Below
Below is also the sample python code from the site (I know you're using C#)
Replace CLIENTSECRETS_LOCATION value with the location of your client_secrets.json file.