C#发送邮件异常:根据验证过程,远程证书无效
2018-06-17 21:55:35来源:未知 阅读 ()
今天在做发送邮件功能时,开始用qq邮箱和163邮箱都可以正常发送,后再改用我公司的邮箱和smtp时竟然报错了。
异常提示-----“根据验证过程,远程证书无效”,后来通过查询资料解决该问题,上代码:
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; } } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP实现好友生日邮件提醒 2018-12-04
- 利用SQLServer数据库发送邮件 2018-06-23
- 3org.springframework.beans.factory.BeanDefinitionStoreEx 2018-06-17
- 关于MySQL存储过程异常处理的一点心得 2018-06-17
- mysql datetime查询异常 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash