以下是控制器中的代码:
[HttpPost, ValidateInput(true)]
public ActionResult Contact(ContactForm model)
{
if (!ModelState.IsValid)
{
return Json("error");
}
else
{
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.UserName = //email here
WebMail.Password = //password here
WebMail.EnableSsl = true;
string message = model.Name + " " + model.Telephone + " " + model.Email + " " + model.Address + " " + model.City + " " + model.ZipCode;
try
{
WebMail.Send("example@example.com", "philaslim.com/info email", message, model.Email);
return Json("thanks");
}
catch (Exception ex)
{
return Json(ex.Message.ToString());
}
}
正如我所说,它仅在生产服务器上失败。返回的异常消息是“发送邮件失败”。有什么想法吗?
编辑:InnerException:
System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能在System.Net.Sockets.Socket响应74.125.53.108:25 System.Net.ServicePoint.ConnectSocketInternal上的.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)(布尔值connectFailure,套接字s4,套接字s6,套接字和套接字,IPAddress&地址,ConnectSocketState状态,IAsyncResult asyncResult,Int32超时,异常和异常) - - 内部异常堆栈跟踪的结束---在System.Net.PooledStream的System.Net.ServicePoint.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress& address,Socket& abortSocket,Socket& abortSocket6,Int32 timeout)处。在System.Net.PooledStream.Activate激活(Object owningObject,Boolean async,Int32 timeout,GeneralAsyncDelegate asyncCallback)(Object owningObject,GeneralAsyncDelegate asyncC allback)位于System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)的System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)位于System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)的System System.Net.Mail.SmtpClient.Send上的.Net.Mail.SmtpClient.GetConnection()(MailMessage消息)
答案 0 :(得分:6)
您的外发SMTP端口(25,587)可能已关闭,请检查防火墙设置。