我正在使用 System.Net.Mail。这个领域有一些主题,但它们是 ASP.NET 并且通常与 VB 相关。我将代码隔离为桌面骨架代码,我使用 .NET 3.5 和 C#。因此,发送适用于我尝试过的所有场景(EnableSsl
假/真,UseDefaultCredentials
假/真)。但SendAsync
仅在UseDefaultCredentials
未设置为 true 时才有效(事实证明,如果您将其明确设置为 false,它甚至可能很重要,默认情况下为 false),EnableSsl
为 true(好的,这也可以是服务器设置),并且硬编码我的凭据. 我希望能够SendAsync
使用UseDefaultCredentials
. 代码:
void sendmail() {
MailMessage email = new MailMessage();
email.From = new MailAddress("tcs@software.com");
email.To.Add("tcs@software.com");
email.Subject = "Simple test email subject";
email.Body = "Simple test email body";
string mHost = "mail.software.com";
string mUsername = @"DOMAIN\tcs";
string mPassword = "myfreakinpassword?Really???";
SmtpClient smtpCli = new SmtpClient(mHost);
//smtpCli.UseDefaultCredentials = true;
smtpCli.Credentials = new NetworkCredential(mUsername, mPassword);
smtpCli.EnableSsl = true;
smtpCli.SendCompleted += smtpCli_SendCompleted;
try {
//smtpCli.Send(email);
smtpCli.SendAsync(email, null); // This mofo only works in this combination
}
catch (Exception ex) {
Console.WriteLine(ex);
}
}
void smtpCli_SendCompleted(object sender, AsyncCompletedEventArgs e) {
if (e.Error != null)
Console.WriteLine(e.Error);
}