SmtpClient.EnableSsl 屬性

定義

指定 SmtpClient 是否使用 Secure Sockets Layer (SSL) 加密連線。Specify whether the SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.

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

屬性值

Boolean

如果 SmtpClient 使用 SSL 則為 true,否則為 falsetrue if the SmtpClient uses SSL; otherwise, false. 預設為 falseThe default is false.

範例

下列程式碼範例會建立與 SMTP 伺服器的 SSL 連線,並使用連接傳送電子郵件。The following code example establishes an SSL connection with the SMTP server and uses the connection to send an email.

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 郵件伺服器。The EnableSsl property specifies whether SSL is used to access the specified SMTP mail server.

這個屬性的預設值也可以在電腦或應用程式佈建檔中設定。The default value for this property can also be set in a machine or application configuration file. 對屬性所做的任何變更都會覆 EnableSsl 寫設定檔設定。Any changes made to the EnableSsl property override the configuration file settings.

SmtpClient類別僅支援透過傳輸層安全性安全的 Smtp 服務延伸,如 RFC 3207 中所定義。The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. 在此模式中,SMTP 會話會在未加密的通道上開始,然後用戶端會將 STARTTLS 命令發出至伺服器,以切換到使用 SSL 的安全通訊。In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. 如需詳細資訊,請參閱網際網路工程任務推動小組所發佈的 RFC 3207 (IETF) 。See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.

替代連接方法是在傳送任何通訊協定命令之前,先建立 SSL 會話的位置。An alternate connection method is where an SSL session is established up front before any protocol commands are sent. 此連接方法有時稱為 SMTP/SSL、SMTP over SSL 或 SMTPS,預設會使用埠465。This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. 目前不支援使用 SSL 的替代連接方法。This alternate connection method using SSL is not currently supported.

您可以使用 ClientCertificates 來指定應使用哪些用戶端憑證來建立 SSL 連線。You can use ClientCertificates to specify which client certificates should be used to establish the SSL connection. ServerCertificateValidationCallback 讓您拒絕 SMTP 伺服器所提供的憑證。The ServerCertificateValidationCallback allows you to reject the certificate provided by the SMTP server. SecurityProtocol屬性可讓您指定要使用的 SSL 通訊協定版本。The SecurityProtocol property allows you to specify the version of the SSL protocol to use.

注意

如果 EnableSsl 屬性設定為 true ,而且 SMTP 郵件伺服器未在 STARTTLS 中通告對 EHLO 命令的回應,則對 Send 或方法的呼叫 SendAsync 將會擲回 SmtpExceptionIf the EnableSsl property is set to true, and the SMTP mail server does not advertise STARTTLS in the response to the EHLO command, then a call to the Send or SendAsync methods will throw an SmtpException.

適用於

另請參閱