0

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

4

1 回答 1

1

您可以使用 SmtpClient 类使用 Windows 身份验证发送电子邮件。对于这个只是初始化UseDefaultCredentials属性为真;

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());
}

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

于 2013-05-22T10:26:16.727 回答