c# gmail 连接失败,因为连接的主机未能响应

c# gmail connection failed because connected host has failed to respond

我正在尝试使用以下代码从服务器使用我的 gmail 帐户发送邮件。

            var fromAddress = new MailAddress("xxxxx@gmail.com", "xxxxxx");
        var toAddress = new MailAddress(toMailAddress);
        const string fromPassword = "xxxxx";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            IsBodyHtml = true,
            Subject = mailSubject,
            Body = htmlBody
        })
        {
            smtp.Send(message);
        }

当我将它部署到我的 windows 服务器上时,这给出了下一个结果,尽管我在防火墙上有一个端口 587 的 Inobound 规则。当我在本地测试它时它工作完美

connectionFailed

我也遇到过这个问题。实际上 Gmail 不允许访问 gmail 以发送安全性较低的应用程序的邮件。 执行以下步骤: 如果你遇到像

这样的异常
"An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the 
client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
  1. 转到您的 google 帐户 "My Account"。
  2. 在"Sign-in & security"下面,你会发现"Allow less secure apps: OFF",你需要点击它旁边的按钮打开它。
  3. 现在可以通过您的代码访问您的 gmail 帐户了。