c# - 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);
}
如果您有任何问题,请查看我之前的问题或直接写在下面的评论中。
解决方案
请尝试使用端口号 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
推荐阅读
- android - 在 Android IMA 的伴随广告中显示 Gif 或 HTML
- python - /updatestudent/3 处的 Django ValueError 无法分配“'1'”:“Student.class_id”必须是“Class”实例
- css - CSS 覆盖 - MAT-TABLE - Angular Material 11
- php - 超级奇怪的 PHP 翻译解决方法
- java - 代码相同,但弹出菜单在另一个活动中看起来不同
- db2 - db2 控制台访问问题
- python - Pandas:如何将一列包含多个元组的字符串拆分为多列单独的元组字符串
- typescript - 如何访问对象的值?
- huggingface-transformers - 将两个评估数据集传递给 HuggingFace Trainer 对象
- javascript - 使用 Typescript 在 GraphQL 中传递参数返回一个空的花括号