【问题标题】:Can't send mail from my localhost by using smtp with C# [duplicate]无法使用带有 C# 的 smtp 从我的本地主机发送邮件 [重复]
【发布时间】:2017-07-01 14:11:42
【问题描述】:

我正在做一个网络项目。我想创建“忘记我的密码”系统并在需要时向用户发送邮件,但是当我尝试这样做时,它会给出错误,例如 SmtpException : Failure Sending Message, SocketException: Party did not proper response or connection failed because connected主机未能响应,无法发送邮件。我尝试了 2525、25 和 587 端口。我还检查了是否可以通过 mxtoolbox 连接 smtp.gmail.com:

这是 SMTP 电子邮件服务器测试

这是 SMTP DNS 测试

这是我的代码:

 protected void lblForPass_Click(object sender, EventArgs e)
 {
     SmtpClient client = new SmtpClient();
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.EnableSsl = true;
     client.Host = "smtp.gmail.com";
     client.UseDefaultCredentials = false;
     client.Port = 25;
     client.Timeout = 300000;
     System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("mytestmail@gmail.com", "1122334455");
     client.Credentials = credentials;

     MailMessage msg = new MailMessage();
     msg.From = new MailAddress("mytestmail@gmail.com");
     msg.To.Add(new MailAddress("mytestmail2@gmail.com"));

     msg.Subject = "Subject Test";
     msg.IsBodyHtml = true;
     msg.Body = string.Format("<html><head></head><body><b>Body Test</b></body>");


      try
      {
          client.Send(msg);
          ScriptManager.RegisterClientScriptBlock(this, GetType(), "Mail Status", "alert('Sended Successfuly')", true);
      }

      catch(Exception ex)
      {
          Exception ex2 = ex;
          string errorMessage = string.Empty;
          while (ex2 != null)
          {
              errorMessage += ex2.ToString();
              ex2 = ex2.InnerException;

          }
          string err = ex.Message;
          ScriptManager.RegisterClientScriptBlock(this, GetType(), "Mail Error", "alert('" +err+"')", true);
        }


    }  

我在这个代码块中是否遗漏了什么或者我需要检查其他什么?谢谢。

【问题讨论】:

    标签: c# asp.net smtp mail-server


    【解决方案1】:

    我想你可以试试: 1)尝试将端口更改为587。 2)检查您的帐户设置(https://www.google.com/settings/security/lesssecureapps

    对于我的网络项目,我每月使用http://www.mailgun.com/ 免费发送 10000 封电子邮件。并且可以轻松连接到任何域名、日志记录等

    【讨论】:

    • 已经尝试过端口 587。将我的帐户设置更改为“打开”并尝试过,但也没有帮助。可能是关于我的防火墙吗?
    • 是的。在我看来,使用第三方服务是一个很好的解决方案。
    猜你喜欢
    • 2012-05-25
    • 2020-02-07
    • 1970-01-01
    • 2017-01-24
    • 2014-01-10
    • 1970-01-01
    • 2014-01-17
    • 2015-05-18
    • 2011-06-16
    相关资源
    最近更新 更多