The Server Response Was: 5.5.1 Authentication Required In Gmail

Introduction

Today, I came across a forum post saying, "User is not able to send a mail to his domain that he purchased but able to send mail to the gmail". The actual problem is that gmail wants the clients to be secure even though we set EnableSsl = true;
 
Simply, it wont work.

To fix the issue, add the RemoteCertificateValidationCallback. Now, it will work fine. 
  1. public class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string senderID = "[email protected]";  
  6.             string senderPassword = "xxxx";  
  7.             RemoteCertificateValidationCallback orgCallback = ServicePointManager.ServerCertificateValidationCallback;  
  8.             string body = "Test";  
  9.             try  
  10.             {  
  11.                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidateCertificate);  
  12.                 ServicePointManager.Expect100Continue = true;  
  13.                 MailMessage mail = new MailMessage();  
  14.                 mail.To.Add("[email protected]");  
  15.                 mail.From = new MailAddress(senderID);  
  16.                 mail.Subject = "My Test Email!";  
  17.                 mail.Body = body;  
  18.                 mail.IsBodyHtml = true;  
  19.                 SmtpClient smtp = new SmtpClient();  
  20.                 smtp.Host = "smtp.gmail.com";  
  21.                 smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword);  
  22.                 smtp.Port = 587;  
  23.                 smtp.EnableSsl = true;  
  24.                 smtp.Send(mail);  
  25.                 Console.WriteLine("Email Sent Successfully");  
  26.             }  
  27.             catch (Exception ex)  
  28.             {  
  29.                 Console.WriteLine(ex.Message);  
  30.             }  
  31.             finally  
  32.             {  
  33.                 ServicePointManager.ServerCertificateValidationCallback = orgCallback;  
  34.             }  
  35.         }  
  36.   
  37.         private static bool OnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  
  38.         {  
  39.             return true;  
  40.         }  
  41.     }
Ebook Download
View all
Learn
View all