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] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Combine Two Tables 联合两表
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- .NET跨平台之旅:升级至ASP.NET 5 RC1,Linux上访问SQL Server数据库
今天微软正式发布了ASP.NET 5 RC1(详见Announcing ASP.NET 5 Release Candidate 1),.NET跨平台迈出了关键一步. 紧跟这次RC1的发布,我们成功地将 ...
- 2016BUAA校赛决赛
A. 题意:有n个点,n-1条边,1-2-3-4-5-...-n,每条边都有权值,代表走这条边的时间,时刻0一个人在点1,问从时刻1~m,有哪些时刻这个人可能走到n点 分析:将每条边当作物品,可以选1 ...
- 【MySQL】mysql workbench
1.导入,导出,数据之间的传输[两台服务器]2.连接管理3.服务器管理4.表的管理
- 多线程之异步编程: 经典和最新的异步编程模型,async与await
经典的异步编程模型(IAsyncResult) 最新的异步编程模型(async 和 await) 将 IAsyncInfo 转换成 Task 将 Task 转换成 IAsyncInfo 示例1.使用经 ...
- ORB-SLAM(六)回环检测
上一篇提到,无论在单目.双目还是RGBD中,追踪得到的位姿都是有误差的.随着路径的不断延伸,前面帧的误差会一直传递到后面去,导致最后一帧的位姿在世界坐标系里的误差有可能非常大.除了利用优化方法在局部和 ...
- hibernate主键生成策略
在hibernate中,提供了多种主键生成器(不同的数据库,不同的表结构使用的主键生成策略也不相同),查阅相关资料经过实验总结如下: 1.increment 主键按照数值顺序递增,使用当前实例中最大值 ...
- layer.open打开iframe页面的调用父页面方法及关闭
//调用父类方法 window.parent.exportData($('#shownum').val(),$('#splitstr').val()); //关闭iframe页面var index = ...