【发布时间】:2020-11-03 00:22:08
【问题描述】:
我试图在我的EmailConnectionInfo 中使用EnableSsl = false,但似乎用于smtp 连接的smtp client 正在尝试使用SSL,因为默认SecureSocketOptions 设置为Auto。当我手动创建客户端并将the overload 与SecureSocketOptions = None 一起使用时,它起作用了。
错误:
Failed to send email: MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection.
The SSL certificate presented by the server is not trusted by the system for one or more of the following reasons:
1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. The certificate presented by the server is expired or invalid.
See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate for possible solutions. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
解决方法:
我将ServerCertificateValidationCallback 设置为true
在我的Program.cs 中使用时,它可以工作:
.UseSerilog((hostingContext, loggerConfiguration) =>{
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
.WriteTo.Email(
new EmailConnectionInfo {
FromEmail = "{email}",
ToEmail = "{email}",
MailServer = "{SMTP IP}",
Port = {PORT},
EnableSsl = false,
EmailSubject = "Something to log",
ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => true
},
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
batchPostingLimit: 1,
restrictedToMinimumLevel: LogEventLevel.Warning);
});
但是,当我尝试将 Email sink 的所有 serilog 设置包含在 appsettings.json 中时,"ServerCertificateValidationCallback": "(senderX, certificate, chain, sslPolicyErrors) => true" 无法读取(我的假设)
我的appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.File","Serilog.Sinks.Email" ],
"MinimumLevel": "Information",
"WriteTo": [
...
{
"Name": "Email",
"Args": {
"connectionInfo": {
"MailServer": "{SMTP IP}",
"Port": {PORT},
"EnableSsl": false,
"FromEmail": "{email}",
"ToEmail": "{email}",
"EmailSubject": "Something went wrong",
"ServerCertificateValidationCallback": "(s, cert, chain, sslPolicyErrors) => true"
},
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
"restrictedToMinimumLevel": "Warning",
"batchPostingLimit": 1
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
...
有什么想法吗?
编辑:我打开的githut issue
【问题讨论】:
-
您连接的 SMTP 端口是什么?您要连接到哪个电子邮件服务器,它是在本地吗?
-
嗨 @CaioProiete 端口是 25,它是一个 Exchange Server 并且它在本地。
-
对。正如您在source code 中看到的,当
enableSsl是false并且您的Exchange 服务器中可以使用TLS 时,接收器使用SecureSocketOptions.StartTlsWhenAvailable...
标签: asp.net-core serilog appsettings