添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 接收者邮箱列表 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:认证失败。

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

VB.NET代码

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

2.把证书认证函数OnRemoteCertificateValidationCallback返回值True

1 // 忽略证书认证错误处理的函数
2 private bool OnRemoteCertificateValidationCallback(
3   Object sender,
4   X509Certificate certificate,
5   X509Chain chain,
6   SslPolicyErrors sslPolicyErrors)
8   return true;  // 认证正常,没有错误
  
' 忽略证书认证错误处理的函数
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

转载于:https://www.cnblogs.com/chuancheng/p/10001687.html

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.X509Ce...
System.Net.ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate) Public Function ValidateServ...
A(对方公司)提供接口服务,地址时http前缀的,加上用户名和密码等数据后测试时没有问题,可以直接调通,但在切换到生产后,地址变更为https前缀,然后这个接口就调不到。 产生问题分析: https前缀会有ssl证书验证,在post调取该地址时,可以忽略掉该验证,否则会产生调不到的情况。 解决办法: 代码调用时需在调用地址前加忽略掉ssl验证这段代码,如下: ServicePointManager.ServerCertificateValidationCallback = (sender,
C#中,HRESULT:0x80131040异常通常是由于程序集的版本不兼容所致。这可能是由于程序集的版本与当前运行时版本不兼容,或者由于程序集引用了其他程序集,而这些程序集的版本与当前运行时版本不兼容。为了解决这个问题,可以尝试以下几种方法: 1.检查程序集的版本是否与当前运行时版本兼容。可以通过在Visual Studio中打开项目属性并检查目标框架版本来检查当前运行时版本。 2.检查程序集引用的其他程序集的版本是否与当前运行时版本兼容。可以通过在Visual Studio中打开项目引用并检查每个引用的版本来检查。 3.如果程序集是从其他计算机上编译的,则可能需要重新编译程序集以使用当前运行时版本。 4.如果以上方法都无法解决问题,则可以尝试使用程序集绑定日志记录来确定问题的根本原因。可以通过在应用程序配置文件中启用程序集绑定日志记录来完成此操作。 ```csharp <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <logToConsole enabled="true"/> </assemblyBinding> </runtime> </configuration>