クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
System.Net.Mail 名前空間
SmtpClient クラス
SmtpClient プロパティ
 Credentials プロパティ

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.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 サーバーでは、サーバーがクライアントに代わって電子メールを送信する前に、クライアントが認証されている必要があります。既定のネットワーク資格情報を使用するには、このプロパティを設定する代わりに UseDefaultCredentialstrue に設定できます。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
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker