【发布时间】: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