【问题标题】:Gmail SMTP is raising errors when i publish my web application on a hosting server当我在托管服务器上发布我的 Web 应用程序时,Gmail SMTP 引发错误
【发布时间】:2015-11-17 08:05:44
【问题描述】:

我有以下使用 gmail smtp 发送电子邮件的操作方法:-

   [HttpPost]
            public ActionResult Contact(Contact c)
        {

            //
            if (ModelState.IsValid)
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("*****");
                mail.To.Add(new MailAddress("******"));
                mail.Subject = "New Feedback from Edama Web Site";
                mail.IsBodyHtml = true;
                System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
                mailBody.AppendLine("<b>Dear Sirs, </b><br/><br/>");

                mail.Body = mailBody.ToString();
                // Create a new Smpt Client using Google's servers
                var mailclient = new SmtpClient();
                mailclient.Host = "smtp.gmail.com";
                mailclient.Port = 587;

                // This is the critical part, you must enable SSL
                mailclient.EnableSsl = true;
                mailclient.Credentials = new System.Net.NetworkCredential
                     ("***", "***"); /

                //Or your Smtp Email ID and Password
                //smtp.EnableSsl = true;
                mailclient.Send(mail);




                TempData["message"] = string.Format("Your Feedback has been submitted. Thanks");
                return RedirectToAction("Contact");
            }
            return View(c);

        }

现在,当我在 VS 2013 下运行我的 Web 应用程序时,一切都会正常运行,我将正确接收电子邮件。但是现在我在托管服务器下部署了我的 Web 应用程序,当我调用上述操作方法时,出现以下错误:-

 The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

那么是什么导致了这个错误?

第二个问题。我有点困惑,因为在我的操作方法中,我定义了以下 mailclient.EnableSsl = true; 这是否意味着我的 Web 应用程序应该在 https 下运行,这不是我的情况,因为我的 Web 应用程序是一个公共网站所以它是不在 https 下 .. 正如我之前提到的,当我在 VS 2013 中调用我的操作方法时,例如 localhost:1234 即使我没有在 https 下运行我的本地主机,smtp 也会正确发送电子邮件 ...

【问题讨论】:

  • @Taleeb 如果我将端口号更改为 465,即使从 VS @ 运行我的应用程序,我也会收到此异常 - 无法从传输连接读取数据:net_io_connectionclosed。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。源错误:第 196 行:mailclient.Send(mail);

标签: asp.net asp.net-mvc asp.net-mvc-5 smtp gmail


【解决方案1】:

使用 ssl 而不是 gmail smtp - 465 中使用的端口号。 源:https://support.google.com/a/answer/176600?hl=en

在发送电子邮件时启用 SSL 属性不需要您的应用程序通过 ssl 运行。而是指定是否使用 SSL 访问指定的 SMTP 邮件服务器。

【讨论】:

  • 现在我将端口从 587 更改为 465 ,但即使我在 Visual Studio 中运行项目,我也会收到此异常:- 无法从传输连接读取数据:net_io_connectionclosed。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.IO.IOException:".........虽然在 VS 上运行我的项目时使用 587 将起作用,但在我将应用程序托管到托管提供商时不起作用
  • 尝试设置 mailClient.UseDefaultCredentials = false。同时启用 telnet 并从命令提示符尝试“telnet smtp.gmail.com 465”
  • 这也不起作用。我添加了“mailclient.UseDefaultCredentials = false;”我尝试使用端口 587 和端口 465 。另外,如果我使用端口 465,那么当我在 Visual Studio 中运行项目时,即使发送电子邮件也将无法工作,我将在其中收到此异常“System.dll 中发生'System.Net.Mail.SmtpException'类型的异常但不是在用户代码中处理附加信息:操作已超时。"
  • 根据此处的答案 (stackoverflow.com/questions/26113404/…) - 您需要设置 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; gmail smtp 工作。此外,您是否收到来自 google 的任何电子邮件,表明它被阻止或其他什么?
  • 我添加了“smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network”,如果我在 VS 中运行我的应用程序,它再次运行良好,但如果我在托管应用程序上尝试这个,我会收到这个错误...
猜你喜欢
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2019-08-21
相关资源
最近更新 更多