SmtpClient.EnableSsl 屬性

定義

指定 SmtpClient 是否使用 Secure Sockets Layer (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 的安全通訊。 如需詳細資訊,請參閱網際網路標準組織 (IETF) 發佈的 RFC 3207。

替代連線方法是在傳送任何通訊協定命令之前預先建立 SSL 工作階段。 此連線方法有時稱為 SMTP/SSL、SMTP over SSL 或 SMTPS,預設會使用埠 465。 目前不支援使用 SSL 的替代連線方法。

您可以使用 ClientCertificates 來指定應該用來建立 SSL 連線的用戶端憑證。 ServerCertificateValidationCallback可讓您拒絕 SMTP 伺服器所提供的憑證。 屬性 SecurityProtocol 可讓您指定要使用的 SSL 通訊協定版本。

注意

EnableSsl如果 屬性設定true為,且 SMTP 郵件伺服器不會在回應 EHLO 命令中公告 STARTTLS,則 Send 呼叫 或 SendAsync 方法會擲回 SmtpException

適用於

另請參閱