电子邮件提醒未发送

时间:2015-09-01 15:57:17

标签: c# asp.net-mvc email smtp reminders

我是MVC新手并尝试创建手动电子邮件提醒。这是一个按钮,将在每天开始时按下,向患者发送提醒电子邮件,告知他们最后一次约会是在2年前。然而,似乎没有任何东西发送。出于测试目的,我将提醒更改为今天,而不是2年前。

控制器:

    public ActionResult Email2Year()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Email2Year(Appointment model)
    {
        if (ModelState.IsValid)
        {
            var query = (from d in db.Appointments
                where d.Date == DateTime.Now.Date
                select d);

            foreach (var d in query)
            {

            var body = "<p>Hi {0}</p><p>Appointment reminder, You last appointment was {1}. We recomend you make an appointment every 2 year....";
            var message = new MailMessage();
            message.To.Add(new MailAddress(d.Patient.User.Email)); 
            message.From = new MailAddress("XXXXX@gmail.com");  
            message.Subject = "Your email subject";
            message.Body = string.Format(body, d.Patient.User.FirstName, d.Date);
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "XXXXX@gmail.com",  // replace with valid value
                    Password = "XXXXXXX"  // replace with valid value
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                await smtp.SendMailAsync(message);
            }
                return RedirectToAction("Sent");
            }
        }
        return View(model);
    }

    public ActionResult Sent()
    {
        return View();
    }
}

任何建议都将不胜感激。感谢

0 个答案:

没有答案