.NET Framework 類別庫
SmtpClient..::.Credentials 屬性

更新:2007 年 11 月

取得或設定用來驗證寄件者的認證。

命名空間:  System.Net.Mail
組件:  System (在 System.dll 中)

語法

Visual Basic (宣告)
Public Property Credentials As ICredentialsByHost
Visual Basic (使用方式)
Dim instance As SmtpClient
Dim value As ICredentialsByHost

value = instance.Credentials

instance.Credentials = value
C#
public ICredentialsByHost Credentials { get; set; }
Visual C++
public:
property ICredentialsByHost^ Credentials {
    ICredentialsByHost^ get ();
    void set (ICredentialsByHost^ value);
}
J#
/** @property */
public ICredentialsByHost get_Credentials()
/** @property */
public  void set_Credentials(ICredentialsByHost value)
JScript
public function get Credentials () : ICredentialsByHost
public function set Credentials (value : ICredentialsByHost)

屬性值

型別:System.Net..::.ICredentialsByHost

ICredentialsByHost,表示用於驗證的認證,如果未指定認證則為 nullNothingnullptrNull 參照 (即 Visual Basic 中的 Nothing)

例外狀況

例外狀況條件
InvalidOperationException

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

備註

某些 SMTP 伺服器要求先驗證用戶端,然後伺服器才會代表該用戶端傳送電子郵件。若要使用您的預設網路認證,可以將 UseDefaultCredentials 設定為 true,而不用設定這個屬性。如果 UseDefaultCredentials 屬性設定為 false, 連接到伺服器時,將會使用 Credentials 屬性中設定的值進行認證。如果 UseDefaultCredentials 屬性設定為 false 但尚未設定 Credentials 屬性,則會以匿名方式將郵件傳送到伺服器。

您也可以使用應用程式和電腦組態檔來指定認證資訊。如需詳細資訊,請參閱<mailSettings> 項目 (網路設定)。如果資訊是使用 Credentials 屬性來指定,則這個資訊會覆寫組態檔設定。

警告:

如果您提供用於基本驗證的認證,則認證會以純文字方式傳送到伺服器。這可能會出現安全性問題,因為其他人可能看到並利用您的認證。

範例

在下列程式碼範例中,示範了設定用於傳送電子郵件的認證。

C#
        public static void CreateTestMessage1(string server, int port)
        {
            string to = "jane@contoso.com";
            string from = "ben@contoso.com";
            string subject = "Using the new SMTP client.";
            string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
            MailMessage message = new MailMessage(from, to, subject, body);
            SmtpClient client = new SmtpClient(server, port);
            // Credentials are necessary if the server requires the client 
            // to authenticate before it will send e-mail on the client's behalf.
            client.Credentials = CredentialCache.DefaultNetworkCredentials;

            client.Send(message);
        }
Visual C++
   static void CreateTestMessage1( String^ server, int port )
   {
      String^ to = L"jane@contoso.com";
      String^ from = L"ben@contoso.com";
      String^ subject = L"Using the new SMTP client.";
      String^ body = L"Using this new feature, you can send an e-mail message from an application very easily.";
      MailMessage^ message = gcnew MailMessage( from,to,subject,body );
      SmtpClient^ client = gcnew SmtpClient( server,port );

      // Credentials are necessary if the server requires the client 
      // to authenticate before it will send e-mail on the client's behalf.
      client->Credentials = CredentialCache::DefaultNetworkCredentials;

      //Display the properties on the service point.
      ServicePoint^ p = client->ServicePoint;
      Console::WriteLine( L"Connection lease timeout: {0}", p->ConnectionLeaseTimeout );
      client->Send( message );
      client->~SmtpClient();
   }


J#
public static void CreateTestMessage1(String server, int port)
{
    String to = "jane@contoso.com";
    String from = "ben@contoso.com";
    String subject = "Using the new SMTP client.";
    String body = "Using this new feature, you can send an e-mail message"
        + " from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server, port);
    // Credentials are necessary if the server requires the client 
    // to authenticate before it will send e-mail on the client's behalf.
    client.set_Credentials(CredentialCache.get_DefaultNetworkCredentials());
    //Display the properties on the service point.
    ServicePoint p = client.get_ServicePoint();
    Console.WriteLine("Connection lease timeout: {0}", 
    System.Convert.ToString(p.get_ConnectionLeaseTimeout()));
    client.Send(message);
} //CreateTestMessage1
平台

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

版本資訊

.NET Framework

支援版本:3.5、3.0、2.0
請參閱

參考

標記 :


Page view tracker