Friday, July 22, 2016

The server response was: 5.5.1 authentication required in Gmail

Introduction 
          Today i came  across an forum post saying  cant able to send a mail to his domain that he purchased but able to send mail to gmail to gmail.The actual is gmail wants the clients to be secure even throw we set EnableSsl = true;
simply it wont work.
       
           To fix the issue add the RemoteCertificateValidationCallback then it will work fine.
  1. public class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string senderID = "xxx@gmail.com";  
  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("danish.habib@plan-international.org");  
  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.     }  
  

No comments :

Post a Comment