【问题标题】:ASP.NET Send EmailASP.NET 发送电子邮件
【发布时间】:2012-12-11 01:07:32
【问题描述】:

当用户点击联系我们页面中的提交时,我试图发送一封电子邮件,但由于某种原因它不起作用,我做错了什么? (此代码 sn-p 中省略了 PS 电子邮件和密码,但包含在实际解决方案中。

谢谢

web.config 中的代码:

<system.net>
<mailSettings>
  <smtp from="order@test.com">
    <network host="smtp.gmail.com"
             userName="" //my email
             password="" //password deleted for privacy reasons
             defaultCredentials="false"
             port="456"
             enableSsl="true" />
  </smtp>
</mailSettings>

asp.net 联系表单中的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(new MailAddress("test@gmail.com"));
        mail.Subject = "Test";
        mail.Body = "Message was sent from" + txtName.text + txtComment.text;
        SmtpClient smtp = new SmtpClient();
        smtp.SendAsync(mail, null);
    }

【问题讨论】:

  • 不工作是什么意思??
  • SendAsync()改成Send()看看异常。
  • 页面似乎加载了很长一段时间,但从未收到电子邮件。我也尝试设置 Async="true" 仍然无法正常工作

标签: asp.net email


【解决方案1】:

Gmails 使用端口 465,而不是 456 用于 SSL SMTP。来自here

外发邮件 (SMTP) 服务器 - 需要 TLS1 或 SSL:smtp.gmail.com

使用身份验证:是

TLS/STARTTLS 端口:587

SSL 端口:465

【讨论】:

  • 您是否按照 Slaks 的建议捕获了异常?另外,您是否拥有test@gmail.com 或者您是否为这篇文章更改了它? (因为那是您发送邮件的地方)
  • test@gmail.com 更改为电子邮件,是的,但没有抛出异常
  • System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应
  • 检查您的防火墙没有阻止该端口。
  • 关闭防火墙 - 还是一样的问题
【解决方案2】:

“其他”可能的原因:

  • 您的代码在本地开发时是否“工作”,但在您将文件“发布”/ftp/copy 等时停止工作?

  • 如果 Yes:检查您主机的trust ASP.Net 设置。如果是medium trust(可能在共享主机中),请注意您中等信任度中不能使用除端口 25 以外的任何端口用于 SMTP强>.

它在本地 (dev) 工作,因为在本地 dev/VS 环境中,ASP.Net 在full trust 中运行。

REF(MSDN 博客):SMTP Problems when ASP.Net is not running in full-trust

【讨论】:

  • System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应
  • @rikket 没有说您应该使用端口 25 - Google Mail 在端口 25 上不起作用。我是说 如果(并且仅当)@987654326 @ 已设置,您不能使用端口 25 以外的任何其他端口(因此无法与 Google Mail 一起使用)。
猜你喜欢
  • 2016-09-15
  • 1970-01-01
  • 2012-07-27
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 2011-05-24
相关资源
最近更新 更多