Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. public ActionResult Index()
  2. {
  3. return View();
  4. }
  5.  
  6. [HttpPost]
  7. [ValidateAntiForgeryToken]
  8. public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
  9. {
  10. if (ModelState.IsValid)
  11. {
  12. List<string> paths = new List<string>();
  13.  
  14. foreach (var file in files)
  15. {
  16. if (file.ContentLength > 0)
  17. {
  18. var fileName = Path.GetFileName(file.FileName);
  19. var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/uploads"), fileName);
  20. file.SaveAs(path);
  21. paths.Add(path);
  22. }
  23. }
  24.  
  25. var message = new MailMessage();
  26. foreach (var path in paths)
  27. {
  28. var fileInfo = new FileInfo(path);
  29. var memoryStream = new MemoryStream();
  30. using (var stream = fileInfo.OpenRead())
  31. {
  32. stream.CopyTo(memoryStream);
  33. }
  34. memoryStream.Position = 0;
  35. string fileName = fileInfo.Name;
  36. message.Attachments.Add(new Attachment(memoryStream, fileName));
  37. }
  38.  
  39. //Rest of business logic here
  40. string EncodedResponse = Request.Form["g-Recaptcha-Response"];
  41. bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
  42. if (IsCaptchaValid)
  43. {
  44.  
  45. var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Subject:</b> {2} </p><p><b>Message:</b></p><p>{3}</p><p><b>Software Description:</b></p><p>{4}</p>";
  46. message.To.Add(new MailAddress("")); // replace with valid value
  47. message.From = new MailAddress(""); // replace with valid value
  48. message.Subject = "(Inquire for SELLING)";
  49. message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message, model.Desc);
  50. message.IsBodyHtml = true;
  51. using (var smtp = new SmtpClient())
  52. {
  53. var credential = new NetworkCredential
  54. {
  55. UserName = "", // replace with valid value
  56. Password = "" // replace with valid value
  57. };
  58. smtp.Credentials = credential;
  59. smtp.Host = "smtp.live.com";
  60. smtp.Port = 587;
  61. smtp.EnableSsl = true;
  62. smtp.SendCompleted += (s, e) =>
  63. {
  64. //delete attached files
  65. foreach (var path in paths)
  66. System.IO.File.Delete(path);
  67. };
  68. await smtp.SendMailAsync(message);
  69. ViewBag.Message = "Your message has been sent!";
  70.  
  71. ModelState.Clear();
  72. return View("Index");
  73. }
  74. } else
  75.  
  76. {
  77. TempData["recaptcha"] = "Please verify that you are not a robot!";
  78. }
  79.  
  80. } return View(model);
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement