我看到了构造器的不同版本,一种使用来自web.config的信息,一种指定主机,一种使用主机和端口。但是,如何将用户名和密码设置为不同于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());
}
}