从谷歌发送电子邮件在生产服务器上失败但从localhost可以正常工作

bob*_*bek 5 c# asp.net asp.net-mvc-3

这是控制器中的代码:

[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());
                }

            }
Run Code Online (Sandbox Code Playgroud)

正如我所说,它只在生产服务器上失败.返回的异常消息是"发送邮件失败".有任何想法吗?

编辑:InnerException:

System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接主机而建立连接失败未能在System.Net.SService.ConnectSocketInternal(布尔connectFailure,套接字s4,套接字s6,套接字和套接字,IP地址和地址, ConnectSocketState状态,IAsyncResult asyncResult,Int32 timeout,Exception&exception)---内部异常堆栈跟踪结束---在System.Net.ServicePoint.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress&address,Socket&abortSocket,Socket&abortSocket6) System.Net.PooledStream.Activate(Obj)上的System.Net.PooledStream.Activate(Object owningObject,Boolean async,Int32 timeout,GeneralAsyncDelegate asyncCallback)处于Int32超时 位于System.Net.Mail.SmtpTransport.GetConnection(ServicePoint)的System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)上的System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)中的ect owningObject,GeneralAsyncDelegate asyncCallback) System.Net.Mail.SmtpClient.Send(MailMessage消息)中的System.Net.Mail.SmtpClient.GetConnection()处的servicePoint)

ric*_*ott 6

您的外发SMTP端口(25,587)可能已在服务器上关闭,请检查防火墙设置.