【问题标题】:ASP.NET cannot sent email from smtpASP.NET 无法从 smtp 发送电子邮件
【发布时间】:2019-01-23 00:16:16
【问题描述】:

back to my previous question。我仍然无法通过 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);
                    }

如果你有任何问题,请看我之前的问题或者直接写在下面的cmets中。

【问题讨论】:

  • 你试过587端口吗?此外,电子邮件发送功能在主机服务器和本地开发机器上都不起作用?
  • @MohsinMehmood 是的,我尝试了所有端口,但它在服务器上也不起作用

标签: c# asp.net email smtp gmail


【解决方案1】:

请尝试使用端口号 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

【讨论】:

  • 这真的很奇怪,它根本不起作用(Gmail 和 Yahoo)
  • 请试试这个,MailMessage mail = new MailMessage(from, to); mail.Subject = "主题"; mail.Body = "Body";//Console.ReadLine(); SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.端口 = 587; smtp.Credentials = new NetworkCredential("email-id", "password"); smtp.EnableSsl = true; smtp.发送(邮件);如果您的正文不包含任何 HTML 代码,则设置 isHtmlBody = false;
  • 我也在 yahoo 中打开了不太安全的应用程序,它可以在这个网站上运行。但仍然无法在我的应用程序中使用。 SMTP 主机:smtp.mail.yahoo.com 端口:587 使用 SLL:True 使用身份验证:True
猜你喜欢
  • 1970-01-01
  • 2022-07-20
  • 2022-01-21
  • 2013-09-06
  • 2017-02-15
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多