· 10 years ago · Aug 09, 2016, 10:16 AM
1[HttpPost]
2 [ValidateAntiForgeryToken]
3 public async Task<ActionResult> Contact(EmailFormModel model)
4 {
5 if (ModelState.IsValid)
6 {
7 var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
8 var message = new MailMessage();
9 message.To.Add(new MailAddress("recipient@gmail.com")); // replace with valid value
10 message.From = new MailAddress("***@gmail.com"); // replace with valid value
11 message.Subject = "Your ticket booked successfully";
12 message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
13 message.IsBodyHtml = true;
14
15 using (var smtp = new SmtpClient())
16 {
17 var credential = new NetworkCredential
18 {
19 UserName = "***@gmail.com", // replace with valid value
20 Password = "***" // replace with valid value
21 };
22 smtp.Credentials = credential;
23 smtp.Host = "smtp.gmail.com";
24 smtp.Port = 587;
25 smtp.EnableSsl = true;
26 await smtp.SendMailAsync(message);
27 //smtp.Send(message);
28 return RedirectToAction("Sent");
29 }
30 }
31 return View(model);
32
33 }
34 public ActionResult Sent()
35 {
36 return View();
37 }