SmtpClient.UseDefaultCredentials 屬性

定義

取得或設定 Boolean 值,控制是否隨著要求傳送 DefaultCredentials

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

屬性值

如果使用預設認證,則為 true,否則為 false。 預設值是 false

例外狀況

正在傳送電子郵件時,無法變更這個屬性的值。

範例

下列程式代碼範例示範如何使用這個屬性。

static void CreateTestMessage2( String^ server )
{
   String^ to = L"jane@contoso.com";
   String^ from = L"ben@contoso.com";
   MailMessage^ message = gcnew MailMessage( from,to );
   message->Subject = L"Using the new SMTP client.";
   message->Body = L"Using this new feature, you can send an email message from an application very easily.";
   SmtpClient^ client = gcnew 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->Send( message );
   client->~SmtpClient();
}
public static void CreateTestMessage2(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;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
            ex.ToString());
    }
}

備註

某些 SMTP 伺服器會要求客戶端在伺服器代表其傳送電子郵件之前進行驗證。 如果伺服器要求這個SmtpClient物件,請使用目前登入使用者的預設認證進行驗證時,請將此屬性true設定為 。 對於用戶端應用程式,這是大部分案例中所需的行為。

您也可以使用應用程式和電腦組態檔來指定認證資訊。 如需詳細資訊,請參閱 <mailSettings> 元素 (網路設定)

UseDefaultCredentials如果 屬性設定false,為 ,則在連接到伺服器時,屬性中Credentials設定的值將會用於認證。 UseDefaultCredentials如果屬性設定false為 ,且Credentials屬性尚未設定,則會以匿名方式將郵件傳送至伺服器。

警告

如果您提供認證進行基本驗證,則會以純文字形式將它們傳送給伺服器。 這可能會顯示安全性問題,因為您的認證可以看見,然後供其他人使用。

適用於