我看到不同版本的構造函數,一個使用web.config中的信息,一個指定主機,一個指定主機和端口。但是,我如何將用戶名和密碼設置爲與web.config不同的內容?我們有問題,我們的內部smtp被一些高安全性客戶端阻止,我們想使用他們的smtp服務器,有沒有辦法從代碼而不是web.config做到這一點?如何在.NET中爲SmtpClient對象設置用戶名和密碼?
在這種情況下,如果沒有任何數據庫可用,我將如何使用web.config憑據,例如?
public static void CreateTestMessage1(string server, int port)
{
string to = "[email protected]";
string from = "[email protected]";
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());
}
}
如果我沒有設定一個將它默認使用那些從web.config中? – BigOmega 2010-05-04 16:43:59
不,它將是DefaultNetworkCredentials(如果提供)或者什麼都不是。 – pipelinecache 2010-05-04 22:10:11
@pipelinecache Ryan是對的,如果沒有提供憑據,它將在web.config中查看 –
Tomas
2012-02-03 12:03:51