简体   繁体   English

NET Core Mailkit 无法发送多封电子邮件

[英]NET Core Mailkit Cannot send multiple email

I am using MailKit to send email in a .NET Core 3.1 project.我正在使用 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);
}

The function is wait for a minute then get error at this line: smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);该函数等待一分钟,然后在这一行出现错误: smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

2020-10-15 15:20:31.457 +07:00 [Error] An unhandled exception has occurred while executing the request.
MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection.

This usually means that the SSL certificate presented by the server is not trusted by the system for one or more 
of the following reasons:

1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
4. The certificate presented by the server is expired or invalid.

Then I changed the SecureSocketOptions to SecureSocketOptions.Auto: smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.Auto);然后我将 SecureSocketOptions 更改为 SecureSocketOptions.Auto: smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.Auto); The first SendEmail (send toEmail1) worked, but the second (send toEmail2) got the same error as when using SecureSocketOptions.StartTls .第一个 SendEmail (send toEmail1) 工作,但第二个 (send toEmail2) 得到与使用SecureSocketOptions.StartTls时相同的错误。 Then I run again the function, the first SendEmail also got the same error.然后我再次运行该函数,第一个 SendEmail 也出现了同样的错误。 I wait for few minutes and run the function again, the first SendEmail worked, the second email got error.我等了几分钟再次运行该功能,第一个 SendEmail 工作,第二个电子邮件出错。

Could someone please suggest a solution?有人可以提出解决方案吗?

Thank you.谢谢你。

The problem that you are facing is that the SMTP server does not like you connecting and reconnecting so quickly.您面临的问题是 SMTP 服务器不喜欢您如此快速地连接和重新连接。

What you need to do is to re-use the same SmtpClient connection to send multiple messages, like this:您需要做的是重新使用同一个 SmtpClient 连接来发送多条消息,如下所示:

public void SendEmail(SmtpClient smtp, string fromEmail, 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 };

    smtp.Send(email);
}

public void SendEmail()
{
    using var smtp = new SmtpClient();
    smtp.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate(fromEmail, fromEmailPassword);

    SendEmail(smtp, fromEmail, toEmail1, subject, html);
    SendEmail(smtp, fromEmail, toEmail2, subject, html);

    smtp.Disconnect(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM