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;
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.
- public class Program
- {
- static void Main(string[] args)
- {
- string senderID = "[email protected]";
- 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("[email protected]");
- 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;
- }
- }

Gautam YadavPosted Jun 7, 2018, 11:37 PM
I get values from html page to aspx page
Gautam YadavPosted Jun 7, 2018, 11:36 PM
Here is my code SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "asdf84264yadav"); MailMessage mail1 = new MailMessage("[email protected]", Request.Form["email"].ToString()); mail1.Subject = "Enquiry Received from : " + Request.Form["fullname"].ToString(); string Body = "Name : " + Request.Form["fullname"].ToString() + "\nMail Id : " + Request.Form["email"].ToString() + "\nMail Id : " + Request.Form["message"].ToString(); mail1.Body = Body; mail1.IsBodyHtml = true; smtp.Send(mail1);
Gautam YadavPosted Jun 7, 2018, 11:35 PM
Sir i enable less secure app but error is also come
Ashish KumarPosted Feb 14, 2018, 4:04 AM
I used the same Code but System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
VenkatPosted Jul 27, 2016, 2:10 AM
Thank you bro
kalu singh raoPosted Jul 27, 2016, 1:56 AM
Nice share