首页 > 解决方案 > ASP.NET 无法从 smtp 发送电子邮件

问题描述

回到我之前的问题。我仍然无法通过 Gmail SMTP 发送电子邮件。以前我有错误:

邮件发送失败。

但是现在我更改了一些代码,现在我遇到了他的错误:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应 64.233.167.109:465

我很生气,我开始使用wireshark 来了解问题出在哪里。

这是我发现的:

smtp.gmail.com 服务器:53.90.35.60 地址:53.90.35.60#53

非权威答案:smtp.gmail.com 规范名称 = gmail-smtp-msa.l.google.com。名称:gmail-smtp-msa.L.google.com 地址:64.233.167.108 名称:gmail-smtp-msa.L.google.com 地址:64.233.167.109

据我了解,从邮件 smtp 服务器收到了响应

我们也在我们的 Intranet 中使用代理,但我在我的应用程序中添加了代理,在 Web.config 中:

<system.net>
<defaultProxy enabled="true">
  <proxy proxyaddress="proxy_was_here" bypassonlocal="true"
  />
</defaultProxy>

这是新代码的一部分:

var fromAddress = new MailAddress("email", "From Name");
                    var toAddress = new MailAddress("email", "To Name");
                    const string fromPassword = "pass";
                    const string subject = "Subject";
                    const string body = "Body";

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

如果您有任何问题,请查看我之前的问题或直接写在下面的评论中。

标签: c#asp.netemailsmtpgmail

解决方案


请尝试使用端口号 587 和 EnableSSL = false

var smtp = new SmtpClient
{
   Host = "smtp.gmail.com",
   Port = 587,
   EnableSsl = true,
   DeliveryMethod = SmtpDeliveryMethod.Network,
   UseDefaultCredentials = false,
   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
 };

还要检查您在https://www.smtper.net/中的凭据

并且还允许从此链接中使用不太安全的应用程序:https ://myaccount.google.com/lesssecureapps?pli=1


推荐阅读