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 ...
随机推荐
- C#调用vbs脚本实现Windows版Siri
最近新加入,把自己一些有意思的小东西分享给大家,我是一个学生,代码写得少,哪里不规范,希望大家见谅. 这事我封装好的一个类,可以直接实例化对象之后,调用"对象.Talk()"方法, ...
- oracle日常——sqlplus客户端登录
1.进入cmd 2.命令--sqlplus--提示输入帐号与密码 3.进入后,就可以直接键入sql命令 ps.sql命令后面需要添加分号后才可以回车执行
- 关于List的ConcurrentModificationException
对ArrayList的操作我们可以通过索引象来访问,也可以通过Iterator来访问,只要不对ArrayList结构上进行修改都不会造成ConcurrentModificationException, ...
- python3 @classmethod 的使用场合
官方的说法: classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @clas ...
- theano broadcasting
当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...
- RSA加密例子和中途遇到的问题
在进行RSA加密例子 package test; import java.io.IOException; import java.security.Key; import java.security. ...
- 深入浅出学Spring Data JPA
第一章:Spring Data JPA入门 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map ...
- 一键批量添加材质的法线贴图-unity插件
有时候材质做完后需要更改贴图,或者增加贴图,数量少的时候可以一张张添加和修改,数量多的时候就只能代码生成了.原理是通过名字的关联:主贴图和法线贴图大多数只是后缀的不同上,如果不是那是美术规范没做好啊, ...
- Python socket编程之八:阶段性总结
f1.py # -*- coding: utf-8 -*- import sqlalchemy import tushare import pandas import socket import st ...
- wifi万能钥pc版提示手机未连接
关于PC版万能钥匙的用法 大部分人都是在“未连接到手机”再不知道怎么搞了 怎么连接到手机呢? 首先要把你的手机变成路由器 让电脑连上 这个都知道怎么搞吧 手机版万能钥匙有个一键让手机变成热点 再用电 ...