usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Mail;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Net.Security;namespaceSmtp{publicclassMail{privatestring sender;/// <summary>/// 发送人/// </summary>publicstring Sender{get{return sender;}set{sender =value;}}privatestring senderMail;/// <summary>/// 发送人邮箱/// </summary>publicstring SenderMail{get{return senderMail;}set{senderMail =value;}}privatestring senderPw;/// <summary>/// 发送人密码/// </summary>publicstring SenderPw{get{return senderPw;}set{senderPw =value;}}privatestring smtpServer;/// <summary>/// smtpServer地址/// </summary>publicstring SmtpServer{get{return smtpServer;}set{smtpServer =value;}}/// <summary>/// /// </summary>/// <param name="sender">委托发件人</param>/// <param name="sender">委托发件人邮件地址</param>/// <param name="senderPw">密码</param>/// <param name="ewsUrl">smtpServer地址</param>publicMail(string sender,string senderMail,string senderPw,string smtpServer){this.sender = sender;this.senderMail = senderMail;this.senderPw = senderPw;this.smtpServer = smtpServer;}publicMail(){}/// <summary>/// 发送邮件/// </summary>/// <param name="title"></param>/// <param name="body"></param>/// <param name="receiver"></param>publicvoidSend(string title,string body,string[] receiver){SmtpClient _smtpClient =newSmtpClient();_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式_smtpClient.Host =this.smtpServer;;//指定SMTP服务器_smtpClient.EnableSsl =true;_smtpClient.Port =587;_smtpClient.Credentials =newSystem.Net.NetworkCredential(this.sender,this.senderPw);//用户名和密码for(int i =0; i < receiver.Length; i++){//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);ServicePointManager.ServerCertificateValidationCallback =delegate(Object obj,X509Certificate certificate,X509Chain chain,SslPolicyErrors errors){returntrue;};MailMessage _mailMessage =newMailMessage(this.senderMail, receiver[i]);_mailMessage.Subject = title;//主题_mailMessage.Body = body;//内容_mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码_mailMessage.IsBodyHtml =true;//设置为HTML格式_mailMessage.Priority = MailPriority.High;//优先级//Attachment am = new Attachment(attachment, "attachment");//_mailMessage.Attachments.Add(am);_smtpClient.Send(_mailMessage);}}publicstaticboolValidateServerCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors){//if (sslPolicyErrors == SslPolicyErrors.None)// return true;//else// return true;// If there are no errors, then everything went smoothly.if(sslPolicyErrors == SslPolicyErrors.None)returntrue;// Note: MailKit will always pass the host name string as the `sender` argument.//var host = (string)sender;string host = sender asstring;if(host ==null){// Handle the case where sender is not a stringConsole.WriteLine("Sender parameter is not a valid string.");returnfalse;}if((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors)!=0){// This means that there are errors in the certificate chain. You can inspect the chain errors and handle them accordingly.foreach(X509ChainStatus chainStatus in chain.ChainStatus){Console.WriteLine("Certificate chain error: "+ chainStatus.StatusInformation);}returnfalse;}if((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable)!=0){// This means that the remote certificate is unavailable. Notify the user and return false.Console.WriteLine("The SSL certificate was not available for {0}", host);returnfalse;}if((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch)!=0){// This means that the server's SSL certificate did not match the host name that we are trying to connect to.var certificate2 = certificate asX509Certificate2;var cn = certificate2 !=null? certificate2.GetNameInfo(X509NameType.SimpleName,false): certificate.Subject;Console.WriteLine("The Common Name for the SSL certificate did not match {0}. Instead, it was {1}.", host, cn);returnfalse;}// The only other errors left are chain errors.Console.WriteLine("The SSL certificate for the server could not be validated for the following reasons:");// The first element's certificate will be the server's SSL certificate (and will match the `certificate` argument)// while the last element in the chain will typically either be the Root Certificate Authority's certificate -or- it// will be a non-authoritative self-signed certificate that the server admin created. foreach(var element in chain.ChainElements){// Each element in the chain will have its own status list. If the status list is empty, it means that the// certificate itself did not contain any errors.if(element.ChainElementStatus.Length ==0)continue;Console.WriteLine("\u2022 {0}", element.Certificate.Subject);foreach(var error in element.ChainElementStatus){// `error.StatusInformation` contains a human-readable error string while `error.Status` is the corresponding enum value.Console.WriteLine("\t\u2022 {0}", error.StatusInformation);}}returnfalse;}/// <summary>/// 发送邮件,带附件/// </summary>/// <param name="title"></param>/// <param name="body"></param>/// <param name="receiver"></param>/// <param name="attachment">附件</param>publicvoidSend(string title,string body,string receiver,System.IO.Stream attachment){SmtpClient _smtpClient =newSmtpClient();_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式_smtpClient.Host =this.smtpServer;;//指定SMTP服务器_smtpClient.Credentials =newSystem.Net.NetworkCredential(this.sender,this.senderPw);//用户名和密码MailMessage _mailMessage =newMailMessage(this.senderMail, receiver);_mailMessage.Subject = title;//主题_mailMessage.Body = body;//内容_mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码_mailMessage.IsBodyHtml =true;//设置为HTML格式_mailMessage.Priority = MailPriority.High;//优先级Attachment am =newAttachment(attachment,"attachment");_mailMessage.Attachments.Add(am);_smtpClient.Send(_mailMessage);}}}
测试调用,Program类
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceSendMail{classProgram{staticvoidMain(string[] args){Smtp.Mail mail =newSmtp.Mail();mail.Sender ="Test";mail.SenderMail ="Test@mail.com";mail.SenderPw ="Pass@word";mail.SmtpServer ="IP地址";//收件人列表string temp ="test@mail.com;test2@mail.com;";string[] res = temp.Split(';');string content ="邮件内容";for(int i =0; i <2; i ++){Console.WriteLine("开始发送第"+ i.ToString());mail.Send("邮件测试 "+ i.ToString(), content, res);}Console.ReadLine();}}}
推送到TestFlight后邮件收到警告信息如下,主要关于新的隐私政策需要补充:
Hello, We noticed one or more issues with a recent submission for TestFlight review for the following app: AABBCC Version 10.10.10 Build 10 Although submission for …