首页 > 解决方案 > 使用 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 的需求?

标签: c#winformssmtpmailkit

解决方案


useSslSecureSocketOptions.

当您通过trueforuseSsl时,它映射到SecureSocketOptions.SslOnConnect.

当您通过falseforuseSsl时,它映射到SecureSocketOptions.StartTlsWhenAvailable.


推荐阅读