5.7.0 用户未在 asp.net core1 中使用 mailkit 和 strato 服务器进行身份验证

5.7.0 User not authenticated in asp.net core1 with mailkit and strato server

在 asp.net 核心 1 应用程序中,我正在尝试使用 mailkit 库(版本 1.8.1)将电子邮件发送到 strato 服务器。我的代码:

  var emailMessage = new MimeKit.MimeMessage();
  try
  {
      emailMessage.From.Add(new MimeKit.MailboxAddress("Support", "some@test.com"));
      emailMessage.To.Add(new MimeKit.MailboxAddress("", "test@test.com"));
      emailMessage.Subject = "Subject";
      var bodyBuilder = new MimeKit.BodyBuilder();
      bodyBuilder.HtmlBody = @"<b>Some body</b>";

      emailMessage.Body = bodyBuilder.ToMessageBody(); 

      using (var client = new MailKit.Net.Smtp.SmtpClient())
      {
          client.Timeout = 15000;
          // Accept all SSL certificates (in case the server supports STARTTLS)
          client.ServerCertificateValidationCallback = (s, c, h, e) => true;

          await client.ConnectAsync("smtp.strato.de", 587, MailKit.Security.SecureSocketOptions.Auto);
          // Note: since we don't have an OAuth2 token, disable
          // the XOAUTH2 authentication mechanism.
          client.AuthenticationMechanisms.Remove("XOAUTH2");

          // Note: only needed if the SMTP server requires authentication
          await client.AuthenticateAsync(login, passord);

          await client.SendAsync(emailMesage);//here error!
          await client.DisconnectAsync(true);
      }
      return true;
  }
  catch (System.Exception)
  {
      return false;
  } 

当我尝试发送电子邮件时,我收到 5.7.0 User not authenticated 错误,尽管身份验证已通过。同样的代码,例如,gmail 工作正常。

有什么解决办法吗?

client.EnableSsl = true;   Add this to your code

Element (Network Settings)

电子邮件 (SMTP) 错误代码

5.5.1 Syntax error, command unrecognized.

5.5.0 Sender e-mail address already specified.

5.5.1 Need MAIL before RCPT.

5.4.4 Host not found (not in DNS).

5.1.1 Mailbox does not exist.

5.1.3 Bad e-mail address syntax.

>   5.7.0 Your IP address is in a blacklist.

5.1.8 Sender e-mail address domain does not exist.

5.1.8 Sender address rejected by address filter.

5.7.1 Relaying denied.

问题是当 SMTP 客户端在验证后发送 EHLO 命令时,smtp.strato.de 会重置其验证状态,即使规范强烈建议客户端应该这样做。

也就是说,smtp.strato.de坏了。

也就是说,您可以通过执行以下操作解决此问题:

client.QueryCapabilitiesAfterAuthenticating = false;

只要在认证之前将属性设置为假,它就可以工作。