smtpClient仅适用于localhost

时间:2014-09-22 18:53:12

标签: c# asp.net-mvc smtp

我的网站上有发送邮件功能。在localhost上调试时,一切正常。但是当站点发布到Web时,每次我尝试发送邮件时,SendMail函数都会在浏览器中返回此错误消息:"您的请求超时。请重试请求。"

为什么它适用于localhost,但不适用于实时发布的网站?可能是什么问题?

这是我的mvc-controller中的代码:

[HttpPost]
    public ActionResult SendMail(HomeContentPage model)
    {
        const string username = "myMailAdress@gmail.com";
        const string password = "mySecretPassword";
        var loginInfo = new NetworkCredential(username, password);
        var msg = new MailMessage();

        var smtpClient = new SmtpClient("smtp.gmail.com", 587)
        {
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = loginInfo
        };

        string message = model.SendMail.SenderName + " have a message for you:<br /><br /><br />" + model.SendMail.Message
                         + "<br /><br />Avs. Phone: " + model.SendMail.Phone
                         +"<br /><br />Avs. Mail: " + model.SendMail.SenderAddress;
        var page = RavenSession.Load<ContentPage>(model.Id) as HomeContentPage;
        try
        {
            msg.From = new MailAddress("dwfff@hello.com", "MySite.com");
            msg.To.Add(new MailAddress("myMailAdress@gmail.com"));
            msg.Subject = "MySite.com";
            msg.Body = message;
            msg.IsBodyHtml = true;

            smtpClient.Send(msg);

            page.SendMail.IsSuccess = true;
            page.Url = page.Url + "#contact";

            return View(page.Template.Name, page);
        }
        catch (Exception)
        {
            return View(page.Template.Name, page);
        }
    }

0 个答案:

没有答案