使用C#发送电子邮件而不使用硬编码用户名和密码并使用域凭据

时间:2013-05-22 10:22:58

标签: c# email

有没有办法用C#发送使用Windows域名USername和密码的电子邮件,我不必明确提及用户名和密码

1 个答案:

答案 0 :(得分:0)

您可以使用SmtpClient类使用Windows身份验证发送电子邮件。这只是init UseDefaultCredentials属性为true;

string to = "jane@contoso.com";
string from = "ben@contoso.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client  
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;

try 
{
  client.Send(message);
}  
catch (Exception ex) 
{
  Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
}

这将要求您的应用程序以管理员身份运行,并且有权代表指定的发件人发送电子邮件。