发布到iis后,电子邮件功能不起作用

时间:2019-05-15 14:33:59

标签: asp.net

我已经发布了我的应用程序,它现在可以从IIS运行。问题是在IIS上运行时,控制器中的电子邮件功能不再起作用(电子邮件功能不发送电子邮件)

我的mvc应用程序具有电子邮件功能,当我运行它时,它确实会发送电子邮件。我发布我的应用程序后,问题开始了,它现在在IIS上运行,但是电子邮件功能不再发送电子邮件了。我该如何解决这个问题?

[HttpPost]
        public JsonResult SendMailToUser(string lt, string reason, string name, string fr, string ed)
        {
            bool result = false;
            result = SendMail("ntulisakhile8@gmail.com", "Leave Reaquest", "Hi Sensei,<br />I would like to take a " + lt + " leave<br/><strong>From:</strong> " + fr + " <strong>To:</strong> " + ed + " <br/>Reason: " + reason + "<br/>Regards<br/>" + name + "<br/><a href=~/Response.html>Respond</a>");

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public bool SendMail(string toEmail, string subject, string emailBody)
        {
            try
            {
                string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
                string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();

                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Timeout = 100000;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(senderEmail, senderPassword);

                MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
                mailMessage.IsBodyHtml = true;
                mailMessage.BodyEncoding = UTF8Encoding.UTF8;
                client.Send(mailMessage);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

0 个答案:

没有答案