SmtpClient.Send 正在运行,但 SmtpClient.SendMailAsync 无法在 smtp 上运行。office365.com
SmtpClient.Send is working, but SmtpClient.SendMailAsync is not working on smtp.office365.com
我开发了两种方法:分别使用 SmtpClient.Send 和 SmtpClient.SendMailAsync 的 SendEmailBySmtp() 和 SendEmailAsyncBySmtp()。
目前第一种方法有效,但第二种方法无效不是。它没有错误,但没有电子邮件。
我该如何解决?
class Program
{
static void Main(string[] args)
{
SendEmailBySmtp();
SendEmailAsyncBySmtp();
}
static void SendEmailBySmtp()
{
MailMessage message = new MailMessage()
{
From = new MailAddress("test@example.com", "Test User"),
Subject = "Subject",
Body = "Body"
};
message.To.Add("test@example.com");
message.CC.Add("test@example.com");
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using (SmtpClient client = new SmtpClient())
{
client.Port = 587;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("test@example.com", "password");
client.Send(message);
}
}
static async Task SendEmailAsyncBySmtp()
{
MailMessage message = new MailMessage()
{
From = new MailAddress("test@example.com", "Test User"),
Subject = "Subject",
Body = "Body"
};
message.To.Add("test@example.com");
message.CC.Add("test@example.com");
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using (SmtpClient client = new SmtpClient())
{
client.Port = 587;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("test@example.com", "password");
await client.SendMailAsync(message);
}
}
}
问题出在您忘记等待第二次调用的 Main
方法中。因为在 main 方法中你不能使用 await 关键字你必须手动 "await" 线程
像下面那样做:
static void Main(string[] args)
{
SendEmailBySmtp();
SendEmailAsyncBySmtp().GetAwaiter().GetResult();
}
你的问题的答案是:程序在SendMailAsync
完成工作之前结束,所以电子邮件发送操作在发送之前停止。
我开发了两种方法:分别使用 SmtpClient.Send 和 SmtpClient.SendMailAsync 的 SendEmailBySmtp() 和 SendEmailAsyncBySmtp()。
目前第一种方法有效,但第二种方法无效不是。它没有错误,但没有电子邮件。
我该如何解决?
class Program
{
static void Main(string[] args)
{
SendEmailBySmtp();
SendEmailAsyncBySmtp();
}
static void SendEmailBySmtp()
{
MailMessage message = new MailMessage()
{
From = new MailAddress("test@example.com", "Test User"),
Subject = "Subject",
Body = "Body"
};
message.To.Add("test@example.com");
message.CC.Add("test@example.com");
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using (SmtpClient client = new SmtpClient())
{
client.Port = 587;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("test@example.com", "password");
client.Send(message);
}
}
static async Task SendEmailAsyncBySmtp()
{
MailMessage message = new MailMessage()
{
From = new MailAddress("test@example.com", "Test User"),
Subject = "Subject",
Body = "Body"
};
message.To.Add("test@example.com");
message.CC.Add("test@example.com");
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using (SmtpClient client = new SmtpClient())
{
client.Port = 587;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("test@example.com", "password");
await client.SendMailAsync(message);
}
}
}
问题出在您忘记等待第二次调用的 Main
方法中。因为在 main 方法中你不能使用 await 关键字你必须手动 "await" 线程
像下面那样做:
static void Main(string[] args)
{
SendEmailBySmtp();
SendEmailAsyncBySmtp().GetAwaiter().GetResult();
}
你的问题的答案是:程序在SendMailAsync
完成工作之前结束,所以电子邮件发送操作在发送之前停止。