使用 Mailkit:"The SMTP server has unexpectedly disconnected."

Using Mailkit : "The SMTP server has unexpectedly disconnected."

我正在尝试使用 SendGrid 的免费 SMTP 中继从我的 ASP.NET 应用程序发送电子邮件。我可以连接到服务器,但是当我尝试进行身份验证时,出现此错误:"The SMTP server has unexpectedly disconnected."

using (var client = new SmtpClient())
        {
            client.ServerCertificateValidationCallback =
                (sender, certificate, certChainType, errors) => true;
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            // connection
            client.Connect("smtp.host", 465, true);
            client.Authenticate("UserName", "Password");//error occurs here

            client.Send(email);
            client.Disconnect(true);
        }

再一次,我可以毫无问题地连接,但是当我尝试进行身份验证时,出现了前面提到的错误...

有什么建议吗?

干杯

我解决了从 SendGrid 更改为 Google 为所有用户提供的免费 SMTP 服务的问题。只需按照步骤 here 你应该可以开始了!

您必须提供:

  • 用户名:是apikey(作为硬编码值'apikey')。
  • 密码:是你从网上生成的apikey,是一个很大的hashy-like字符串。

您可以在他们的 docs 上找到它。但是很难找到。