· 10 years ago · May 18, 2016, 04:18 AM
1public class HomeController : Controller
2{
3 public ActionResult Index()
4 {
5 return View();
6 }
7
8 [HttpPost]
9 [ValidateAntiForgeryToken]
10 public async Task<ActionResult> Index(EmailFormModel model, IEnumerable<HttpPostedFileBase> files)
11 {
12 if (ModelState.IsValid)
13 {
14
15 List<string> paths = new List<string>();
16
17 foreach (var file in files)
18 {
19
20 if (file.ContentLength > 0)
21 {
22 var fileName = Path.GetFileName(file.FileName);
23 var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
24 file.SaveAs(path);
25 paths.Add(path);
26 }
27 }
28
29 var message = new MailMessage();
30 foreach (var path in paths)
31 {
32 var fileInfo = new FileInfo(path);
33 var memoryStream = new MemoryStream();
34 using (var stream = fileInfo.OpenRead())
35 {
36 stream.CopyTo(memoryStream);
37 }
38 memoryStream.Position = 0;
39 string fileName = fileInfo.Name;
40 message.Attachments.Add(new Attachment(memoryStream, fileName));
41 }
42
43 //Rest of business logic here
44 string EncodedResponse = Request.Form["g-Recaptcha-Response"];
45 bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false);
46 if (IsCaptchaValid)
47 {
48
49 var body = "<p>Email From: {0} ({1})</p><p>Subject: {2} </p><p>Message:</p><p>{3}</p>";
50 message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value
51 message.From = new MailAddress("***@ymailcom"); // replace with valid value
52 message.Subject = "Your email subject";
53 message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
54 message.IsBodyHtml = true;
55 using (var smtp = new SmtpClient())
56 {
57 var credential = new NetworkCredential
58 {
59 UserName = "***@gmail.com", // replace with valid value
60 Password = "***" // replace with valid value
61 };
62 smtp.Credentials = credential;
63 smtp.Host = "smtp.gmail.com";
64 smtp.Port = 587;
65 smtp.EnableSsl = true;
66 await smtp.SendMailAsync(message);
67 ViewBag.Message = "Your message has been sent!";
68
69 ModelState.Clear();
70 return View("Index");
71 }
72 }
73 else
74 {
75 TempData["recaptcha"] = "Please verify that you are not a robot!";
76 }
77 }
78 return View(model);
79
80
81 }
82
83 public ActionResult Upload()
84 {
85 var file = Request.Files["Filedata"];
86 string savePath = Server.MapPath(("~/uploads") + file.FileName);
87 file.SaveAs(savePath);
88
89 return Content(Url.Content(("~/uploads") + file.FileName));
90 }
91
92<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script>
93<script type="text/javascript" src="@Url.Content("~/Content/UploadifyContent/jquery.uploadify.js")"></script>
94<script type="text/javascript" src="@Url.Content("~/Content/UploadifyContent/jquery.uploadify.min.js")"></script>
95<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/UploadifyContent/uploadify.css")" />
96
97<script type="text/javascript">
98 $(function () {
99 $('#file_upload').uploadify({
100 'swf': "@Url.Content("~/Content/UploadifyContent/uploadify.swf")",
101 'cancelImg': "@Url.Content("~/Content/UploadifyContent/uploadify-cancel.png")",
102 'uploader': "@Url.Action("Upload", "Home")",
103 'onUploadSuccess': function (file, data, response) {
104 $("#uploaded").append("<img src='" + data + "' alt='Uploaded Image' />");
105 }
106 });
107 });
108</script>
109
110@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
111{
112 @Html.AntiForgeryToken()
113 <div class="col-md-4">
114 <div class="contact_form block">
115 <div class="row">
116 <div class="col-md-12 col-sm-12">
117 <div id="note"></div>
118 </div>
119 </div>
120 <div id="fields">
121
122 <div class="col-md-12 col-sm-6">
123 @Html.LabelFor(m => m.FromName)
124 @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
125 @Html.ValidationMessageFor(m => m.FromName, null, new { @class = "text-danger" })
126 </div>
127 <div class="col-md-12 col-sm-6">
128 @Html.LabelFor(m => m.FromEmail)
129 @Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
130 @Html.ValidationMessageFor(m => m.FromEmail, null, new { @class = "text-danger" })
131 </div>
132 <div class="clear"></div>
133 <div class="col-md-12 col-sm-6">
134 @Html.LabelFor(m => m.FromSubject)
135 @Html.TextBoxFor(m => m.FromSubject, new { @class = "form-control" })
136 @Html.ValidationMessageFor(m => m.FromSubject, null, new { @class = "text-danger" })
137 </div>
138 <div class="col-md-12 col-sm-6">
139 <form action="" method="post" enctype="multipart/form-data">
140 <label for="file1">Attachments</label>
141 <input type="file" name="files" id="file1" multiple />
142 </form>
143 <div id="uploaded"></div>
144 </div>
145 <div class="col-md-12">
146 @Html.LabelFor(m => m.Message)
147 @Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
148 @Html.ValidationMessageFor(m => m.Message, null, new { @class = "text-danger" })
149 </div>
150 <div class="col-md-12">
151 <div>
152 @if ((TempData["recaptcha"]) != null)
153 {
154 <p>@TempData["recaptcha"]</p>
155 }
156 </div>
157 <div class="g-recaptcha" data-sitekey="6LfVHx8TAAAAAMTDxxQrHDCxO1SyXf1GgbgNBZ5a"></div>
158 </div>
159 <div class="col-md-12"><input class="shortcode_button" type="submit" value="Send"></div>
160 </div>
161 </div>
162 </div>
163}