Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
- {
- if (ModelState.IsValid)
- {
- List<string> paths = new List<string>();
- foreach (var file in files)
- {
- if (file.ContentLength > 0)
- {
- var fileName = Path.GetFileName(file.FileName);
- var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
- file.SaveAs(path);
- paths.Add(path);
- }
- }
- var message = new MailMessage();
- foreach (var path in paths)
- {
- var fileInfo = new FileInfo(path);
- var memoryStream = new MemoryStream();
- using (var stream = fileInfo.OpenRead())
- {
- stream.CopyTo(memoryStream);
- }
- memoryStream.Position = 0;
- string fileName = fileInfo.Name;
- message.Attachments.Add(new Attachment(memoryStream, fileName));
- }
- //Rest of business logic here
- 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>Subject: {2} </p><p>Message:</p><p>{3}</p>";
- 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);
- ViewBag.Message = "Your message has been sent!";
- ModelState.Clear();
- return View("Index");
- }
- }
- else
- {
- TempData["recaptcha"] = "Please verify that you are not a robot!";
- }
- }
- return View(model);
- }
- public ActionResult Upload()
- {
- var file = Request.Files["Filedata"];
- string savePath = Server.MapPath(("~/uploads") + file.FileName);
- file.SaveAs(savePath);
- return Content(Url.Content(("~/uploads") + file.FileName));
- }
- <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
- <script type="text/javascript" src="@Url.Content("~/Content/UploadifyContent/jquery.uploadify.js")"></script>
- <script type="text/javascript" src="@Url.Content("~/Content/UploadifyContent/jquery.uploadify.min.js")"></script>
- <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/UploadifyContent/uploadify.css")" />
- <script type="text/javascript">
- $(function () {
- $('#file_upload').uploadify({
- 'swf': "@Url.Content("~/Content/UploadifyContent/uploadify.swf")",
- 'cancelImg': "@Url.Content("~/Content/UploadifyContent/uploadify-cancel.png")",
- 'uploader': "@Url.Action("Upload", "Home")",
- 'onUploadSuccess': function (file, data, response) {
- $("#uploaded").append("<img src='" + data + "' alt='Uploaded Image' />");
- }
- });
- });
- </script>
- @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, null, new { @class = "text-danger" })
- </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, null, new { @class = "text-danger" })
- </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, null, new { @class = "text-danger" })
- </div>
- <div class="col-md-12 col-sm-6">
- <form action="" method="post" enctype="multipart/form-data">
- <label for="file1">Attachments</label>
- <input type="file" name="files" id="file1" multiple />
- </form>
- <div id="uploaded"></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, null, new { @class = "text-danger" })
- </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>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement