更新 : 2007 年 11 月
差出人の認証に使用する資格情報を取得または設定します。
名前空間 :
System.Net.Mail アセンブリ :
System (System.dll 内)
Public Property Credentials As ICredentialsByHost
Dim instance As SmtpClient
Dim value As ICredentialsByHost
value = instance.Credentials
instance.Credentials = value
public ICredentialsByHost Credentials { get; set; }
public:
property ICredentialsByHost^ Credentials {
ICredentialsByHost^ get ();
void set (ICredentialsByHost^ value);
}
/** @property */
public ICredentialsByHost get_Credentials()
/** @property */
public void set_Credentials(ICredentialsByHost value)
public function get Credentials () : ICredentialsByHost
public function set Credentials (value : ICredentialsByHost)
一部の SMTP サーバーでは、サーバーがクライアントに代わって電子メールを送信する前に、クライアントが認証されている必要があります。既定のネットワーク資格情報を使用するには、このプロパティを設定する代わりに UseDefaultCredentials を true に設定できます。UseDefaultCredentials プロパティに false, が設定されると、Credentials プロパティに設定された値が、サーバーに接続する際の資格情報として使用されます。UseDefaultCredentials プロパティに false が設定され、Credentials プロパティが設定されていない場合、メールが匿名でサーバーに送信されます。
資格情報は、アプリケーションまたはコンピュータの構成ファイルを使用して指定することもできます。詳細については、「<mailSettings> 要素 (ネットワーク設定)」を参照してください。Credentials プロパティを使用して情報が指定された場合、この情報によって構成ファイルの設定がオーバーライドされます。
注意 : |
|---|
基本認証で資格情報を提供すると、クリア テキストでサーバーに送信されます。この場合、他のユーザーが資格情報を読み取って利用できてしまうため、セキュリティ上の問題が発生する可能性があります。 |
電子メールの送信に使用する資格情報を設定するコード例を次に示します。
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);
}
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();
}
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
参照