C#发送邮件异常:根据验证过程,远程证书无效
今天在做发送邮件功能时,开始用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 > )
{
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 > )
{
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 > )
{
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 > )
{
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 ;
}
catch (Exception ex)
{
var loger = LogManager.GetLogger(typeof(emailHandle));
loger.Info(string.Format("发送邮件异常,收信邮箱:{0}", this.To[]), ex);
return -;
}
}
}
}
C#发送邮件异常:根据验证过程,远程证书无效的更多相关文章
- C#发送邮件异常:根据验证过程,远程证书无效,何解???
/// <summary> /// 发送邮件 /// </summary> /// <param name="mailSubjct">邮件主题& ...
- asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理
1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...
- [转]在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效
该文原网址:http://www.cnblogs.com/xwgli/p/5487930.html 在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效. 当访问 h ...
- XmlDocument.Load(url) url是https远程时,报错" 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" "根据验证过程,远程证书无效。"
XmlDocument.Load(url) url是https远程时,报错" 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系." "根据验证过程, ...
- System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效。
好久没写博客了,今天突然遇到个神奇的问题. 做好的网站在win10上和Windows sever 2012 上都没有问题,搬到Windows sever 2003上就出现了这么一个错误: Server ...
- post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案
当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系. ---> System.Security.Authentication.A ...
- 在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效。
当访问 https 内容的时候,有时候经常会看到证书错误(不在操作系统的证书信任链中?)的提示,在浏览器中我们可以忽略错误的证书,继续访问网页内容. 但是在 .NET 程序中,需要由代码来判断是否忽略 ...
- The remote certificate is invalid according to the validation procedure 远程证书验证无效
The remote certificate is invalid according to the validation procedure 根据验证过程中远程证书无效 I'm calling ...
- SmtpClient SSL 发送邮件异常排查
上周使用 SmtpCliet 发送邮件测试,在服务端配置 SSL 465 / 993 情况 ,客户端使用 465 SSL 端口发送邮件异常,测试代码如下: System.Net.ServicePoin ...
随机推荐
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- ConcurrentHashMap内存泄漏问题
问题背景 上周,同事写了一段ConcurrentHashMap的测试代码,说往map里放了32个元素就内存溢出了,我大致看了一下他的代码及运行的jvm参数,觉得很奇怪,于是就自己捣鼓了一下.首先上一段 ...
- C语言内存分配方法。
当C程序运行在操作系统上时,操作系统会给每一个程序分配一定的栈空间. 堆为所有程序共有的,需要时需要申请访问. 一.栈 局部变量.函数一般在栈空间中. 运行时自动分配&自动回收:栈是自动管理的 ...
- caffe源码解析
http://blog.csdn.net/lanxuecc/article/details/53186613
- bootstrap-监听滚动实现头部跟随滚动
实现案例 <body data-spy="scroll" data-target="#bs-example-navbar-collapse-1"> ...
- Android Studio导入项目慢的问题
在Github下载的项目,导入studio时非常慢,原因是下载的项目中的gradle与当前gradle不匹配,需要更新包. 解决办法:修改下载包中的文件 1. xxx-project/.idea/gr ...
- 【转】OpenGL多线程创建纹理,附加我的测试结果
原文地址 http://www.cnblogs.com/mazhenyu/archive/2010/04/29/1724190.html 关于这个问题以前只知道多个线程不能同时使用一个RC,结果为了能 ...
- 初次认识 C# win32 api
第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...
- 大数据量冲击下Windows网卡异常分析定位
背景 mqtt的服务端ActiveMQ在windows上,多台PC机客户端不停地向MQ发送消息. 现象 观察MQ自己的日志data/activemq.log里显示,TCP链接皆异常断开.此时尝试从服务 ...