c# - 使用 Mailkit 连接时,我应该何时使用 SecureSockOptions 或 useSsl
问题描述
在使用 MailKit 设置 smtp 时,我对如何使用第三个参数感到困惑。
这是我到目前为止所拥有的:
// *************** SEND EMAIL *******************
using (var client = new MailKit.Net.Smtp.SmtpClient(new ProtocolLogger("smtp.log")))
{
client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
//accept all SSL certificates
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.IsSslEnabled);
client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.AuthType);
if (emailSettings.IsAuthenticationRequired)
{
// Note: only needed if the SMTP server requires authentication
client.Authenticate(emailSettings.SmtpUsername, emailSettings.SmtpPassword);
}
if (emailSettings.TimeOut == 0) emailSettings.TimeOut = 10;
client.Timeout = emailSettings.TimeOut * 1000;
client.Send(message);
client.Disconnect(true);
}
我的困惑在于这一行:
client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort , true);
我可以选择传入 true/false 或 SecureSockOptions。
这是我的表格上的内容:
我不确定我是否了解这两种不同的设置如何影响电子邮件的发送。我假设我对 useSsl 或 SecureSockOptions 使用真/假?我不确定这些是如何协同工作的。
SecureSockOptions 的选项是:
无 自动 SslOnConnect StartTls StartTlsWhenAvailable
这些选项是否否定了对 useSsl 的需求?
解决方案
useSsl
是SecureSocketOptions
.
当您通过true
foruseSsl
时,它映射到SecureSocketOptions.SslOnConnect
.
当您通过false
foruseSsl
时,它映射到SecureSocketOptions.StartTlsWhenAvailable
.
推荐阅读
- android - ContentLoadingProgressBar 在隐藏前的最短时间未显示
- php - 如何使用 JQuery AJAX 发送图像和文本
- r - 指定要与 `install.packages` 一起使用的已安装依赖项的版本
- laravel - 加载模块脚本失败:服务器以“text/html”的非 JavaScript MIME 类型响应
- python - pygame 角色跳跃在学校项目中不起作用
- variables - PowerBI_variables_error 消息“无法将 Text 类型的值转换为 TRUE/FALSE 类型”
- javascript - 跨度悬停不起作用以及如何更改 div 之外元素的属性
- java - 在 Maven 项目中的包之间移动类时的常见问题
- laravel - Laravel:电子邮件未发送
- amazon-web-services - 已验证发送的域我如何验证它以在 AWS SES 中接收