【问题标题】:When should I use SecureSockOptions or useSsl when connecting using Mailkit使用 Mailkit 连接时,我应该何时使用 SecureSockOptions 或 useSsl
【发布时间】:2019-11-28 13:44:24
【问题描述】:

在使用 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 的需求?

【问题讨论】:

    标签: c# winforms smtp mailkit


    【解决方案1】:

    useSslSecureSocketOptions 的简化版。

    当您为useSsl 传递true 时,它映射到SecureSocketOptions.SslOnConnect

    当您将false 传递给useSsl 时,它映射到SecureSocketOptions.StartTlsWhenAvailable

    【讨论】:

    • 好的,这就是我想知道的。 Ty 为我澄清了这一点。
    【解决方案2】:

    看mailkit文档的Connect方法有5种不同的签名(不同的参数)

    在将布尔值传递给 Connect 的情况下,这意味着如果为真则使用 ssl,如果为假则不使用 ssl。没有一种方法可以同时接受 boolean 和 SecureSocketOptions。

    http://www.mimekit.net/docs/html/Overload_MailKit_Net_Smtp_SmtpClient_Connect.htm

    您应该在上面的链接中阅读他们的文档。

    这也可能对他们的文档有用:

    useSsl 参数只控制客户端是否进行 SSL 包装的连接。换句话说,即使 useSsl 参数 为 false,如果邮件服务器支持 SSL/TLS STARTTLS 扩展。

    要禁用所有 SSL/TLS,请使用 Connect(String, Int32, SecureSocketOptions, CancellationToken) 重载,值为 SecureSocketOptions.None 代替。

    【讨论】:

    • 求帮助!
    【解决方案3】:

    当您拥有受信任的内部网络域控制器类型或受信任的盒子时,您应该使用它可能有一个系统包装,即系统本身由于 OSI 模型较低级别而覆盖它。如果我没记错的话,我会建议您亲自使用它,它可以解决一些较旧的传输模型在同步确认手请求时丢失的问题,并且请求的超时值更高。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多