Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Index(EmailFormModel model)
- {
- if (ModelState.IsValid)
- {
- var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
- var message = new MailMessage();
- message.To.Add(new MailAddress("aaa@gmail.com"));
- message.From = new MailAddress("aaa@gmailcom");
- message.Subject = "Your email subject";
- message.Body = string.Format(body, model.FromName, model.FromEmail, model.FromSubject, model.Message);
- message.IsBodyHtml = true;
- if (model.Upload != null && model.Upload.ContentLength > 0)
- {
- message.Attachments.Add(new Attachment(model.Upload.InputStream, Path.GetFileName(model.Upload.FileName)));
- }
- using (var smtp = new SmtpClient())
- {
- var credential = new NetworkCredential
- {
- UserName = "aaa@gmail.com",
- Password = "aaa"
- };
- 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");
- }
- }
- return View(model);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement