SmtpClient.Credentials プロパティ

定義

差出人の認証に使用する資格情報を取得または設定します。

public:
 property System::Net::ICredentialsByHost ^ Credentials { System::Net::ICredentialsByHost ^ get(); void set(System::Net::ICredentialsByHost ^ value); };
public System.Net.ICredentialsByHost? Credentials { get; set; }
public System.Net.ICredentialsByHost Credentials { get; set; }
member this.Credentials : System.Net.ICredentialsByHost with get, set
Public Property Credentials As ICredentialsByHost

プロパティ値

ICredentialsByHost

認証に使用する資格情報を表す ICredentialsByHost。資格情報が指定されなかった場合は null

例外

電子メールが送信中の場合、このプロパティの値は変更できません。

次のコード例は、電子メールの送信に使用する資格情報を設定する方法を示しています。

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 email 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 email 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();
}
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 email 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 email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
}

注釈

一部の SMTP サーバーでは、サーバーがその代わりに電子メールを送信する前に、クライアントを認証する必要があります。 既定のネットワーク資格情報を使用するには、このプロパティを UseDefaultCredentials 設定する代わりに to を true 設定します。 プロパティが UseDefaultCredentials 設定されている場合、サーバーに false, 接続するときに、プロパティに Credentials 設定された値が資格情報に使用されます。 プロパティが UseDefaultCredentials 設定 false されていて、プロパティが Credentials 設定されていない場合、メールは匿名でサーバーに送信されます。

資格情報は、アプリケーションおよびコンピューターの構成ファイルを使用して指定することもできます。 詳細については、mailSettings> 要素 (ネットワーク 設定) を参照してください<。 プロパティを使用して情報を指定した Credentials 場合、この情報は構成ファイルの設定をオーバーライドします。

注意事項

基本認証の資格情報を指定した場合、資格情報はクリア テキストでサーバーに送信されます。 これにより、資格情報が表示され、他のユーザーが使用できるため、セキュリティの問題が発生する可能性があります。

適用対象