Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
- {
- @Html.AntiForgeryToken()
- <div class="col-md-4">
- <div class="contact_form block">
- <div class="row">
- <div class="col-md-12 col-sm-12">
- <div id="note"></div>
- </div>
- </div>
- <div id="fields">
- <div class="col-md-12 col-sm-6">
- @Html.LabelFor(m => m.FromName)
- @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.FromName)
- </div>
- <div class="col-md-12 col-sm-6">
- @Html.LabelFor(m => m.FromEmail)
- @Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.FromEmail)
- </div>
- <div class="clear"></div>
- <div class="col-md-12 col-sm-6">
- @Html.LabelFor(m => m.FromSubject)
- @Html.TextBoxFor(m => m.FromSubject, new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.FromSubject)
- </div>
- <div class="col-md-12">
- @using (Html.BeginForm("Multiple", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
- {
- <div id="multiple">
- <input type="file" class="multiple" name="files" multiple />
- </div>
- <div id="single">
- <input type="file" class="single" name="files" /><br />
- <input type="file" class="single" name="files" /><br />
- <input type="file" class="single" name="files" /><br />
- </div>
- }
- </div>
- <div class="col-md-12">
- @Html.LabelFor(m => m.Message)
- @Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.Message)
- </div>
- <div class="col-md-12">
- <div>
- @if ((TempData["recaptcha"]) != null)
- {
- <p>@TempData["recaptcha"]</p>
- }
- </div>
- <div class="g-recaptcha" data-sitekey="6LfVHx8TAAAAAMTDxxQrHDCxO1SyXf1GgbgNBZ5a"></div>
- </div>
- <div class="col-md-12"><input class="shortcode_button" type="submit" value="Send"></div>
- </div>
- </div>
- </div>
- }
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Index(EmailFormModel model)
- {
- if (ModelState.IsValid)
- {
- string EncodedResponse = Request.Form["g-Recaptcha-Response"];
- bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
- if(IsCaptchaValid)
- {
- var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
- var message = new MailMessage();
- message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
- message.From = new MailAddress("***@ymailcom"); // replace with valid value
- message.Subject = "Your email subject";
- message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
- message.IsBodyHtml = true;
- using (var smtp = new SmtpClient())
- {
- var credential = new NetworkCredential
- {
- UserName = "***@gmail.com", // replace with valid value
- Password = "***" // 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");
- ViewBag.Message = "Your message has been sent!";
- //TempData["message"] = "Message sent";
- ModelState.Clear();
- return View("Index");
- }
- }else
- {
- TempData["recaptcha"] = "Please verify that you are not a robot!";
- }
- }
- return View(model);
- }
- [HttpPost]
- public ActionResult Multiple(IEnumerable<HttpPostedFileBase> files)
- {
- foreach (var file in files)
- {
- if (file != null && file.ContentLength > 0)
- {
- file.SaveAs(Path.Combine(Server.MapPath("/uploads"), Guid.NewGuid() + Path.GetExtension(file.FileName)));
- }
- }
- return View();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement