SmtpClient.EnableSsl 属性

定义

指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。

public:
 property bool EnableSsl { bool get(); void set(bool value); };
public bool EnableSsl { get; set; }
member this.EnableSsl : bool with get, set
Public Property EnableSsl As Boolean

属性值

如果 SmtpClient 使用 SSL,则为 true;否则为 false。 默认值为 false

示例

下面的代码示例与 SMTP 服务器建立 SSL 连接,并使用该连接发送电子邮件。

public static void CreateTestMessage(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an email message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    // Credentials are necessary if the server requires the client
    // to authenticate before it will send email on the client's behalf.
    client.UseDefaultCredentials = true;
                client.EnableSsl = true;
    client.Send(message);
}

注解

属性 EnableSsl 指定是否使用 SSL 访问指定的 SMTP 邮件服务器。

也可以在计算机或应用程序配置文件中设置此属性的默认值。 对 属性所做的任何更改将 EnableSsl 替代配置文件设置。

SmtpClient 类仅支持 RFC 3207 中定义的基于传输层安全性的安全 SMTP 的 SMTP 服务扩展。 在此模式下,SMTP 会话在未加密的通道上开始,然后客户端向服务器发出 STARTTLS 命令,以切换到使用 SSL 的安全通信。 有关详细信息,请参阅 Internet 工程任务组 (IETF) 发布的 RFC 3207。

一种备用连接方法是预先建立 SSL 会话,然后再发送任何协议命令。 此连接方法有时称为 SMTP/SSL、SMTP over SSL 或 SMTPS,默认情况下使用端口 465。 目前不支持这种使用 SSL 的备用连接方法。

可以使用 ClientCertificates 指定应使用哪些客户端证书建立 SSL 连接。 ServerCertificateValidationCallback允许拒绝 SMTP 服务器提供的证书。 属性 SecurityProtocol 允许你指定要使用的 SSL 协议的版本。

注意

如果 属性 EnableSsl 设置为 true,并且 SMTP 邮件服务器在响应 EHLO 命令时不播发 STARTTLS,则对 SendSendAsync 方法的调用将引发 SmtpException

适用于

另请参阅