Today in the Send mail function, start with QQ mailbox and 163 mailbox can send normal, and then use my company's mailbox and SMTP when unexpectedly error.
Exception prompt-----"based on the verification process, the remote certificate is invalid", and then through the query data to resolve the problem, on the code:
usinglog4net;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Mail;usingSystem.Net.Security;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Text;namespacebll{ Public classEmailhandle {Private string_servicetype ="SMTP"; Private string_host; /// <summary> ///Sender's Mailbox/// </summary> Public stringfrom {Get;Set; } /// <summary> ///Recipient Mailbox List/// </summary> Publiclist<string> to {Get;Set; } /// <summary> ///cc Mailbox List/// </summary> Public string[] Cc {Get;Set; } /// <summary> ///Secret Reader Mailbox list/// </summary> Public string[] BCC {Get;Set; } /// <summary> ///Message Subject/// </summary> Public stringSubject {Get;Set; } /// <summary> ///Message Content/// </summary> Public stringBody {Get;Set; } /// <summary> ///whether it is in HTML format/// </summary> Public BOOLisbodyhtml {Get;Set; } /// <summary> ///List of attachments/// </summary> Public string[] Attachments {Get;Set; } /// <summary> ///Mailbox Service Type (Pop3,smtp,imap,mail, etc.), default to SMTP/// </summary> Public stringservicetype {Get{return_servicetype;} Set{_servicetype =value;} } /// <summary> ///Mailbox server, which is a mailbox server based on servicetype and sender if no mailbox server is defined/// </summary> Public stringHost {Get{return_host;} Set{_host =value;} } /// <summary> ///email account (default to Sender's email account)/// </summary> Public stringUserName {Get;Set; } /// <summary> ///mailbox Password (default to sender's mailbox password), default format GB2312/// </summary> Public stringPassword {Get;Set; } /// <summary> ///Mailbox Priority/// </summary> PublicMailpriority mailpriority {Get;Set; } /// <summary> ///message body encoding format/// </summary> PublicEncoding Encoding {Get;Set; } /// <summary> ///constructing parameters, sending messages, using method notes: Calling in public methods/// </summary> Public intSend () {varMailMessage =NewMailMessage (); //read to recipient mailbox List Try { if( This. To! =NULL&& This. To.count >0) { foreach(stringToinch This. To) {if(string. IsNullOrEmpty (To))Continue; MAILMESSAGE.TO.ADD (NewMailAddress (to. Trim ())); } } //read cc cc email address if( This. Cc! =NULL&& This. Cc.length >0) { foreach(varCcinch This. CC) {if(string. IsNullOrEmpty (CC))Continue; MAILMESSAGE.CC.ADD (Newmailaddress (CC. Trim ())); } } //reading attachments mail attachments if( This. Attachments! =NULL&& This. Attachments.length >0) { foreach(varAttachmentinch This. Attachments) {if(string. IsNullOrEmpty (Attachment))Continue; MAILMESSAGE.ATTACHMENTS.ADD (NewAttachment (Attachment)); } } //read the BCC secret copy address. if( This. BCC! =NULL&& This. Bcc.length >0) { foreach(varBccinch This. BCC) {if(string. IsNullOrEmpty (BCC))Continue; MAILMESSAGE.BCC.ADD (NewMailAddress (BCC. Trim ())); } } //read from sender addressMailmessage.from =NewMailAddress ( This. from); //message HeaderEncoding Encoding = encoding.getencoding ("GB2312"); Mailmessage.subject= This. Subject; //whether the message body is in HTML formatmailmessage.isbodyhtml = This. isbodyhtml; //message bodyMailmessage.body = This. Body; Mailmessage.bodyencoding= This. Encoding; //Message PriorityMailmessage.priority = This. mailpriority; //Send Message Code implementation varSmtpClient =NewSmtpClient {Host= This. Host, Enablessl=true, Credentials=NewNetworkCredential ( This. UserName, This. Password)};//add this paragraph before using the company mailbox to send an error: According to the verification process, the remote certificate is invalid//add and then solve the problemServicepointmanager.servercertificatevalidationcallback =Delegate(Object obj, X509Certificate certificate, X509chain chain, sslpolicyerrors errors) {return true; }; //Certificationssmtpclient.send (MailMessage); return 1; } Catch(Exception ex) {varLoger = Logmanager.getlogger (typeof(Emailhandle)); Loger. Info (string. Format ("Send mail exception, email address: {0}", This. to[0]), ex); return-1; } } }}
C # Send Message exception: The remote certificate is not valid according to the verification process