C # An error occurred while sending the Email: the remote certificate is invalid according to the verification process,
When sending mail today, I started to use both my QQ mailbox and my 163 mailbox to send mails normally, and then I used my company's mailbox and smtp to report an error.
Exception prompt ----- "the remote certificate is invalid according to the verification process". Then, the problem is solved by querying the information, and the code is displayed:
Using log4net; using System. collections. generic; using System. linq; using System. net; using System. net. mail; using System. net. security; using System. security. cryptography. x509Certificates; using System. text; namespace BLL {public class emailHandle {private string _ serviceType = "SMTP"; private string _ host; /// <summary> /// sender's email // </summary> public string From {get; set ;}/// <summary> /// Recipient email List /// </summary> public List <string> To {get; set ;} /// <summary> /// Cc email list /// </summary> public string [] Cc {get; set ;} /// <summary> /// list of Bcc email addresses /// </summary> public string [] Bcc {get; set ;} /// <summary> /// Email Subject /// </summary> public string Subject {get; set ;} /// <summary> /// email content /// </summary> public string Body {get; set ;} /// <summary> /// whether it is in HTML format /// </summary> Public bool IsBodyHtml {get; set ;}/// <summary> /// Attachment List /// </summary> public string [] Attachments {get; set ;} /// <summary> /// MAIL service type (Pop3, SMTP, IMAP, MAIL, etc ), the default value is SMTP // </summary> public string ServiceType {get {return _ serviceType;} set {_ serviceType = value ;}} /// <summary> /// email server. If no email server is defined, the email server is composed of serviceType and Sender. // </summary> public string Host {get {return _ host ;} Set {_ host = value ;}/// <summary> // email account (the default is the account of the sender's mailbox) /// </summary> public string UserName {get; set ;}/// <summary> // email password (the default is the sender's email password ), default format: GB2312 /// </summary> public string Password {get; set ;} /// <summary> /// email priority /// </summary> public MailPriority {get; set ;} /// <summary> /// message body Encoding format /// </summary> public Encoding {get; set ;}/// <summary> // construct the Parameter Number of messages. Use method remarks: Call the public method /// </summary> public int Send () {var mailMessage = new MailMessage (); // read To recipient email list try {if (this. to! = Null & this. to. count> 0) {foreach (string to in this. to) {if (string. isNullOrEmpty (to) continue; mailMessage. to. add (new MailAddress (. trim (); }}// read the Cc email address if (this. cc! = Null & this. cc. length> 0) {foreach (var cc in this. cc) {if (string. isNullOrEmpty (cc) continue; mailMessage. CC. add (new MailAddress (cc. trim (); }}// read Attachments email attachment if (this. attachments! = Null & this. attachments. length> 0) {foreach (var attachment in this. attachments) {if (string. isNullOrEmpty (attachment) continue; mailMessage. attachments. add (new Attachment (attachment) ;}// read the Bcc ing address if (this. bcc! = Null & this. bcc. length> 0) {foreach (var bcc in this. bcc) {if (string. isNullOrEmpty (bcc) continue; mailMessage. bcc. add (new MailAddress (bcc. trim (); }}// read the From sender's address mailMessage. from = new MailAddress (this. from); // mail title Encoding encoding = Encoding. getEncoding ("GB2312"); mailMessage. subject = this. subject; // whether the email body is in HTML format mailMessage. isBodyHtml = this. isBodyHtml; // mail body mailMessage. body = this. body; mailMessage. bodyEncoding = this. encoding; // mail priority mailMessage. priority = this. mailPriority; // send the mail code to implement var smtpClient = new SmtpClient {Host = this. host, EnableSsl = true, Credentials = new NetworkCredential (this. userName, this. password)}; // before this section, use the company email to send an error: According to the verification process, the remote certificate is invalid. // solve the problem by adding ServicePointManager. serverCertificateValidationCallback = delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {return true ;}; // smtpClient authentication. send (mailMessage); return 1;} catch (Exception ex) {var loger = LogManager. getLogger (typeof (emailHandle); loger. info (string. format ("sending email exception, email: {0}", this. to [0]), ex); return-1 ;}}}}