コンストラクターのさまざまなバージョンが表示されます。1 つは web.config の情報を使用し、1 つはホストを指定し、もう 1 つはホストとポートを指定します。しかし、ユーザー名とパスワードを web.config とは異なるものに設定するにはどうすればよいでしょうか? 内部 smtp が一部の高セキュリティ クライアントによってブロックされているという問題があり、それらの smtp サーバーを使用したいのですが、web.config の代わりにコードからこれを行う方法はありますか?
この場合、たとえばデータベースから何も利用できない場合、web.config資格情報をどのように使用しますか?
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;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
ex.ToString());
}
}