C#發送郵件異常:根據驗證過程,遠程證書無效


using log4net;
using System;
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>
        /// 發送者郵箱
        /// </summary>
        public string From { get; set; }

        /// <summary>
        /// 接收者郵箱列表
        /// </summary>
        public List<string> To { get; set; }

        /// <summary>
        /// 抄送者郵箱列表
        /// </summary>
        public string[] Cc { get; set; }

        /// <summary>
        /// 秘抄者郵箱列表
        /// </summary>
        public string[] Bcc { get; set; }

        /// <summary>
        /// 郵件主題
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 郵件內容
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 是否是HTML格式
        /// </summary>
        public bool IsBodyHtml { get; set; }

        /// <summary>
        /// 附件列表
        /// </summary>
        public string[] Attachments { get; set; }

        /// <summary>
        /// 郵箱服務類型(Pop3,SMTP,IMAP,MAIL等),默認為SMTP
        /// </summary>
        public string ServiceType
        {
            get { return _serviceType; }
            set { _serviceType = value; }
        }

        /// <summary>
        /// 郵箱服務器,如果沒有定義郵箱服務器,則根據serviceType和Sender組成郵箱服務器
        /// </summary>
        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }

        /// <summary>
        /// 郵箱賬號(默認為發送者郵箱的賬號)
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 郵箱密碼(默認為發送者郵箱的密碼),默認格式GB2312
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 郵箱優先級
        /// </summary>
        public MailPriority MailPriority { get; set; }

        /// <summary>
        ///  郵件正文編碼格式
        /// </summary>
        public Encoding Encoding { get; set; }

        /// <summary>
        /// 構造參數,發送郵件,使用方法備注:公開方法中調用
        /// </summary>
        public int Send()
        {
            var mailMessage = new MailMessage();

            //讀取To  接收者郵箱列表
            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(to.Trim()));
                    }
                }
                //讀取Cc  抄送者郵件地址
                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()));
                    }
                }
                //讀取Attachments 郵件附件
                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));
                    }
                }
                //讀取Bcc 秘抄人地址
                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()));
                    }
                }
                //讀取From 發送人地址
                mailMessage.From = new MailAddress(this.From);

                //郵件標題
                Encoding encoding = Encoding.GetEncoding("GB2312");
                mailMessage.Subject = this.Subject;
                //郵件正文是否為HTML格式
                mailMessage.IsBodyHtml = this.IsBodyHtml;
                //郵件正文
                mailMessage.Body = this.Body;
                mailMessage.BodyEncoding = this.Encoding;
                //郵件優先級
                mailMessage.Priority = this.MailPriority;

                //發送郵件代碼實現
                var smtpClient = new SmtpClient
                {
                    Host = this.Host,
                    EnableSsl = true,
                    Credentials = new NetworkCredential(this.UserName, this.Password)
                };
//加這段之前用公司郵箱發送報錯:根據驗證過程,遠程證書無效
//加上后解決問題
                ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
                //認證
                smtpClient.Send(mailMessage);
                return 1;
            }
            catch (Exception ex)
            {
                var loger = LogManager.GetLogger(typeof(emailHandle));
                loger.Info(string.Format("發送郵件異常,收信郵箱:{0}", this.To[0]), ex);
                return -1;
            }
        }
    }
}

https://www.cnblogs.com/yifengjianbai/p/6128396.html

 

.NET的SSL通信過程中,使用的證書可能存在各種問題,某種情況下可以忽略證書的錯誤繼續訪問。可以用下面的方式跳過服務器證書驗證,完成正常通信。

1.設置回調屬性ServicePointManager.ServerCertificateValidationCallback

  注:這個屬性設置為要用於客戶端的服務器證書的自定義驗證方法

       True:認證成功; False:認證失敗。

C#代碼

1 ServicePointManager.ServerCertificateValidationCallback =
2           new RemoteCertificateValidationCallback(
3                    OnRemoteCertificateValidationCallback); 

 VB.NET代碼

1 ServicePointManager.ServerCertificateValidationCallback = _
2           New RemoteCertificateValidationCallback( _
3                     AddressOf OnRemoteCertificateValidationCallback) 

2.把證書認證函數OnRemoteCertificateValidationCallback返回值True

C#代碼

復制代碼
1 // 忽略證書認證錯誤處理的函數
2 private bool OnRemoteCertificateValidationCallback(
3   Object sender,
4   X509Certificate certificate,
5   X509Chain chain,
6   SslPolicyErrors sslPolicyErrors)
7 {
8   return true;  // 認證正常,沒有錯誤
9 } 
復制代碼

VB.NET代碼

' 忽略證書認證錯誤處理的函數
Private Function OnRemoteCertificateValidationCallback( _
  ByVal sender As Object, _
  ByVal certificate As X509Certificate, _
  ByVal chain As X509Chain, _
  ByVal sslPolicyErrors As SslPolicyErrors _
) As Boolean
  Return True  ' 認證正常,沒有錯誤
End Function

https://www.cnblogs.com/duanh/p/5781839.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM