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.
- public class Program
- {
- static void Main(string[] args)
- {
- string senderID = "xxx@gmail.com";
- string senderPassword = "xxxx";
- RemoteCertificateValidationCallback orgCallback = ServicePointManager.ServerCertificateValidationCallback;
- string body = "Test";
- try
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidateCertificate);
- ServicePointManager.Expect100Continue = true;
- MailMessage mail = new MailMessage();
- mail.To.Add("danish.habib@plan-international.org");
- mail.From = new MailAddress(senderID);
- mail.Subject = "My Test Email!";
- mail.Body = body;
- mail.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword);
- smtp.Port = 587;
- smtp.EnableSsl = true;
- smtp.Send(mail);
- Console.WriteLine("Email Sent Successfully");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- ServicePointManager.ServerCertificateValidationCallback = orgCallback;
- }
- }
- private static bool OnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- return true;
- }
- }
No comments :
Post a Comment