异步发送邮件不起作用
本文关键字:不起作用 异步 | 更新日期: 2023-09-27 18:25:45
我正在尝试以异步方式发送电子邮件,但没有发送的电子邮件
https://dotnetfiddle.net/oFVtN2
由于您的"fire-and-forget",您将立即处理邮件消息;这可能会影响到你。此外,您没有处理邮件客户端。您应该将整个操作放入"fire-and-forget"中,包括SmtpClient的创建和处理。更像这样的东西:
FireAndForgetTask(async cancellationToken =>
{
using(var smtp = new SmtpClient
{
Host = "myhost",
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("myemail", "mypass"),
Timeout = 50000
})
{
using (var message = new MailMessage("myemail", destMail)
{
Subject = subject,
Body = mailBody,
IsBodyHtml = html
})
{
await smtp.SendMailAsync(message);
}
}
}