Cannot send email using Googles smtp.gmail.com mail server. The same code works fine for the outlook.office365.com mail server. I searched and tried all code snippets I could find on this site. Below is a snapshot of the current state of code.
try
{
string sSMTPServer = WebConfigurationManager.AppSettings["SMTPServer"];
string sSMTPUsername = WebConfigurationManager.AppSettings["SMTPUsername"];
string sSMTPPassword = WebConfigurationManager.AppSettings["SMTPPassword"];
System.Net.Mail.SmtpClient emailClient = new System.Net.Mail.SmtpClient();
emailClient.Host = sSMTPServer;
emailClient.Credentials = new System.Net.NetworkCredential(sSMTPUsername, sSMTPPassword);
emailClient.UseDefaultCredentials = true;
emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
emailClient.Port = 587; //produces "the remote certificate is invalid according to the validation procedure."
//emailClient.Port = 465; produces "failure sending email" error
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
emailClient.EnableSsl = true;
emailClient.Send(mailmessage);
emailErrorHandler.errorThrown = false;
emailErrorHandler.message = "Email Sent Successfully!";
}
catch (Exception ex)
{
emailErrorHandler.errorThrown = true;
emailErrorHandler.message = "error: " + ex.Message;
}