· 10 years ago · May 11, 2016, 10:06 AM
1@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
2{
3 @Html.AntiForgeryToken()
4 <div class="col-md-4">
5 <div class="contact_form block">
6 <div class="row">
7 <div class="col-md-12 col-sm-12">
8 <div id="note"></div>
9 </div>
10 </div>
11 <div id="fields">
12
13 <div class="col-md-12 col-sm-6">
14 @Html.LabelFor(m => m.FromName)
15 @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
16 @Html.ValidationMessageFor(m => m.FromName)
17 </div>
18 <div class="col-md-12 col-sm-6">
19 @Html.LabelFor(m => m.FromEmail)
20 @Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
21 @Html.ValidationMessageFor(m => m.FromEmail)
22 </div>
23 <div class="clear"></div>
24 <div class="col-md-12 col-sm-6">
25 @Html.LabelFor(m => m.FromSubject)
26 @Html.TextBoxFor(m => m.FromSubject, new { @class = "form-control" })
27 @Html.ValidationMessageFor(m => m.FromSubject)
28 </div>
29 <div class="col-md-12">
30 @using (Html.BeginForm("Multiple", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
31 {
32 <div id="multiple">
33 <input type="file" class="multiple" name="files" multiple />
34 </div>
35 <div id="single">
36 <input type="file" class="single" name="files" /><br />
37 <input type="file" class="single" name="files" /><br />
38 <input type="file" class="single" name="files" /><br />
39 </div>
40
41 }
42 </div>
43 <div class="col-md-12">
44 @Html.LabelFor(m => m.Message)
45 @Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
46 @Html.ValidationMessageFor(m => m.Message)
47 </div>
48 <div class="col-md-12">
49 <div>
50 @if ((TempData["recaptcha"]) != null)
51 {
52 <p>@TempData["recaptcha"]</p>
53 }
54 </div>
55 <div class="g-recaptcha" data-sitekey="6LfVHx8TAAAAAMTDxxQrHDCxO1SyXf1GgbgNBZ5a"></div>
56 </div>
57
58 <div class="col-md-12"><input class="shortcode_button" type="submit" value="Send"></div>
59
60 </div>
61 </div>
62 </div>
63}
64
65public ActionResult Index()
66{
67 return View();
68}
69
70[HttpPost]
71[ValidateAntiForgeryToken]
72public async Task<ActionResult> Index(EmailFormModel model)
73{
74 if (ModelState.IsValid)
75 {
76 string EncodedResponse = Request.Form["g-Recaptcha-Response"];
77 bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
78 if(IsCaptchaValid)
79 {
80
81 var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
82 var message = new MailMessage();
83 message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
84 message.From = new MailAddress("***@ymailcom"); // replace with valid value
85 message.Subject = "Your email subject";
86 message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
87 message.IsBodyHtml = true;
88 using (var smtp = new SmtpClient())
89 {
90 var credential = new NetworkCredential
91 {
92 UserName = "***@gmail.com", // replace with valid value
93 Password = "***" // replace with valid value
94 };
95 smtp.Credentials = credential;
96 smtp.Host = "smtp.gmail.com";
97 smtp.Port = 587;
98 smtp.EnableSsl = true;
99 await smtp.SendMailAsync(message);
100 //return RedirectToAction("Sent");
101 ViewBag.Message = "Your message has been sent!";
102
103 //TempData["message"] = "Message sent";
104 ModelState.Clear();
105 return View("Index");
106 }
107
108 }else
109 {
110 TempData["recaptcha"] = "Please verify that you are not a robot!";
111 }
112 }
113 return View(model);
114}
115
116[HttpPost]
117public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
118{
119 foreach (var file in files)
120 {
121 if (file != null && file.ContentLength > 0)
122 {
123 file.SaveAs(Path.Combine(Server.MapPath("/uploads"), Guid.NewGuid() + Path.GetExtension(file.FileName)));
124 }
125 }
126 return View();
127}