【问题标题】:Sending Mail while running the application in IIS在 IIS 中运行应用程序时发送邮件
【发布时间】:2016-04-21 00:53:53
【问题描述】:
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(txtEmail.Text.ToString());
            mailMessage.Subject = txtSubject.Text.ToString();
            mailMessage.To.Add(new MailAddress("my@gmail.com"));
            mailMessage.Body = txtSuggestion.Text.ToString();
            mailMessage.IsBodyHtml = true;


            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.gmail.com";
            sc.Port = 587;
            sc.Credentials = new NetworkCredential(txtEmail.Text.ToString(), txtPassword.Text.ToString());
            sc.EnableSsl = true;
            sc.Send(mailMessage);
            lblMessage.Text = "Message Sent Successfully";
            txtSubject.Text = "";
            txtEmail.Text = "";
            txtName.Text = "";
            txtSuggestion.Text = "";
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }

    }

这是我用来发送电子邮件的代码,但每当我尝试运行应用程序时,都会出错。

enter image description here

如果有人可以帮助我为什么会收到错误,那就太好了。

【问题讨论】:

  • 1) 你能发布调用堆栈吗? 2) 使用 Gmail,这解决了我的问题 - support.google.com/accounts/answer/6010255?hl=en
  • 您是否启用了双因素身份验证?
  • 您的Try Block 出现异常,这就是它显示Failure Sending Mail 的原因。删除try Catch block 并发布原始异常是什么......不知道异常类型我们必须猜测解决方案。

标签: asp.net web-applications smtp


【解决方案1】:

使用 SSL 时,请尝试使用端口 465。

https://support.google.com/a/answer/176600

【讨论】:

    【解决方案2】:

    该代码还有另一个问题,与您订阅的代码无关。在设置联系我们表单时,使用此人的电子邮件作为 FROM 字段将导致 DMARC 失败。你可以阅读here

    另外,我不明白您为什么要使用此人的电子邮件地址和密码来连接到 gmail。如果您要发送邮件,则应使用自己的帐户。

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 2019-10-03
      • 1970-01-01
      • 2014-08-24
      • 2015-12-04
      • 1970-01-01
      • 2023-03-30
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多