部署时无法发送电子邮件

时间:2015-04-02 12:05:02

标签: c# asp.net-mvc-5 smtpclient mailmessage

在本地工作时工作正常,但在发布网站时,邮件功能无法正常工作。我怀疑问题可能在于SmtpClient,但我不确定。有谁知道网络在网络上运行时网络的行为方式是否存在差异?

我的代码:

 public ActionResult SendMail(EmailModel model)
    {
        string host = ConfigurationManager.AppSettings.Get("SMTPHOST");
        int port = Convert.ToInt16(ConfigurationManager.AppSettings.Get("SMTPPORT"));
        string receiver = ConfigurationManager.AppSettings.Get("TOADDRESS");
        string password = ConfigurationManager.AppSettings.Get("PASSWORD");

        SmtpClient smtp = new SmtpClient(host, port);
        smtp.Credentials = new NetworkCredential(receiver, password);
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;

        MailMessage mailMsg = null;

        var status = new HttpStatusCode();

        try
        {
            if (ModelState.IsValid)
            {
                mailMsg = new MailMessage(model.from, receiver, model.subject, model.body);
                smtp.Send(mailMsg);

                return PartialView("_About");
            }
            else
            {
                status = HttpStatusCode.BadRequest;
            }
        }
        catch (Exception ex)
        {
            status = HttpStatusCode.BadRequest;
            SendInfoMail("address", "address", "Exception", ex.ToString());
        }

        return new HttpStatusCodeResult(status);
    }

我认为代码会引发异常,但由于这只发生在网站发布时,我无法对其进行调试。

0 个答案:

没有答案