If I enable EnableSsl for smtpclient am getting an exception: "The remote certificate is invalid according to the validation procedure."
It means the server certificate isn't valid by regular validation rules. You can simply use this snippet to by-pass it:
public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } public static void SendMail(string body, string to, string subject, string attachFilename) { MailMessage message = new MailMessage(); message.Priority = MailPriority.Normal; message.To.Add(new MailAddress(to)); message.Subject = subject; message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9"); message.Body = body;
if (attachFilename != string.Empty) { message.Attachments.Add(new Attachment(attachFilename)); }
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy; SmtpClient client = new SmtpClient(); client.EnableSsl = true; client.Send(message); }
Keywords : Smtpclient, asp.net, system.net.mail, EnableSSL, validation.
|