Advertisement
Guest User

Untitled

a guest
May 16th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
  4. {
  5. if (ModelState.IsValid)
  6. {
  7.  
  8. List<string> paths = new List<string>();
  9.  
  10. foreach (var file in files)
  11. {
  12. if (file.ContentLength > 0)
  13. {
  14. var fileName = Path.GetFileName(file.FileName);
  15. var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
  16. file.SaveAs(path);
  17. paths.Add(path);
  18. }
  19. }
  20.  
  21. var message = new MailMessage();
  22. foreach (var path in paths)
  23. {
  24. var fileInfo = new FileInfo(path);
  25. var memoryStream = new MemoryStream();
  26. using (var stream = fileInfo.OpenRead())
  27. {
  28. stream.CopyTo(memoryStream);
  29. }
  30. memoryStream.Position = 0;
  31. string fileName = fileInfo.Name;
  32. message.Attachments.Add(new Attachment(memoryStream, fileName));
  33. }
  34.  
  35. //Rest of business logic here
  36. string EncodedResponse = Request.Form["g-Recaptcha-Response"];
  37. bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
  38. if (IsCaptchaValid)
  39. {
  40.  
  41. var body = "<p>Email From: {0} ({1})</p><p>Subject: {2} </p><p>Message:</p><p>{3}</p>";
  42. message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
  43. message.From = new MailAddress("***@ymailcom"); // replace with valid value
  44. message.Subject = "Your email subject";
  45. message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
  46. message.IsBodyHtml = true;
  47. using (var smtp = new SmtpClient())
  48. {
  49. var credential = new NetworkCredential
  50. {
  51. UserName = "***@gmail.com", // replace with valid value
  52. Password = "***" // replace with valid value
  53. };
  54. smtp.Credentials = credential;
  55. smtp.Host = "smtp.gmail.com";
  56. smtp.Port = 587;
  57. smtp.EnableSsl = true;
  58. await smtp.SendMailAsync(message);
  59. ViewBag.Message = "Your message has been sent!";
  60.  
  61. ModelState.Clear();
  62. return View("Index");
  63. }
  64.  
  65. }
  66. else
  67. {
  68. TempData["recaptcha"] = "Please verify that you are not a robot!";
  69. }
  70. }return View(model);
  71.  
  72.  
  73. }
  74.  
  75. public class ReCaptcha
  76. {
  77. private string m_Success;
  78. [JsonProperty("success")]
  79.  
  80. public string Success
  81. {
  82. get { return m_Success; }
  83. set { m_Success = value;}
  84. }
  85. private List<string> m_ErrorCodes;
  86. [JsonProperty("error-codes")]
  87.  
  88. public List<string> ErrorCodes
  89. {
  90. get { return m_ErrorCodes; }
  91. set { m_ErrorCodes = value; }
  92. }
  93.  
  94. public static string Validate(string EncodedResponse)
  95. {
  96. var client = new System.Net.WebClient();
  97. string PrivateKey = "***";
  98. var reply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
  99. var captchaResponse = JsonConvert.DeserializeObject<ReCaptcha>(reply);
  100. return captchaResponse.Success;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement