【发布时间】:2021-01-30 14:30:19
【问题描述】:
我正在使用 MailKit 在 .NET Core 3.1 项目中发送电子邮件。
public void SendEmail(string fromEmail, string fromEmailPassword, string toEmail, string subject, string html)
{
var email = new MimeMessage();
email.Sender = MailboxAddress.Parse(fromEmail);
email.To.Add(MailboxAddress.Parse(toEmail));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = html };
using var smtp = new SmtpClient();
smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
smtp.Authenticate(fromEmail, fromEmailPassword);
smtp.Send(email);
smtp.Disconnect(true);
}
public void SendEmail()
{
...
SendEmail(fromEmail, fromEmailPassword, toEmail1, subject, html);
SendEmail(fromEmail, fromEmailPassword, toEmail2, subject, html);
}
功能是等待一分钟然后在这一行得到错误:smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
然后我将 SecureSocketOptions 更改为 SecureSocketOptions.Auto:smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.Auto);
第一个 SendEmail (send toEmail1) 有效,但第二个 (send toEmail2) 得到了与使用 SecureSocketOptions.StartTls 时相同的错误。
然后我再次运行该函数,第一个 SendEmail 也得到了同样的错误。我等了几分钟,然后再次运行该函数,第一封 SendEmail 工作,第二封电子邮件出错。
有人可以提出解决方案吗?
谢谢。
【问题讨论】: