【问题标题】:c# gmail connection failed because connected host has failed to respond [closed]c#gmail连接失败,因为连接的主机没有响应[关闭]
【发布时间】:2017-07-18 04:02:12
【问题描述】:

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

            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

【问题讨论】:

标签: c# windows-server-2012-r2


【解决方案1】:

我也遇到过这个问题。实际上,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 帐户“我的帐户”。
  2. 在“登录和安全”下方,您会看到“允许安全性较低的应用:关闭”,您需要点击旁边的按钮将其打开。
  3. 现在可以通过您的代码访问您的 gmail 帐户。

【讨论】:

  • 遗憾的是,这不是解决方案。 “允许不太安全的应用程序”已打开。如果关闭,我不认为我在本地工作。
  • 请分享你得到的完整例外
  • 你会在这里找到异常:i.stack.imgur.com/bBqff.png
  • 您是否也在代码中使用了某些套接字?如果是,请查看它是否可访问。尝试使用您正在使用的环境中的套接字 ping 您尝试连接的地址。
  • 未在代码中使用任何套接字。就是上面的代码。从服务器 Ping 到 108.177.119.109 可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多