Advertisement
Guest User

Jason

a guest
Feb 8th, 2010
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Mvc.Ajax;
  7. using OpenSkyMedia.Models;
  8. using System.Web.UI;
  9. using System.Net;
  10. using System.Net.Mail;
  11.  
  12. namespace OpenSkyMedia.Controllers
  13. {
  14.     public class EmailMeController : Controller
  15.     {
  16.         //
  17.         // GET: /EmailMe/
  18.  
  19.         [AcceptVerbs(HttpVerbs.Get)]
  20.         public ActionResult Index()
  21.         {
  22.             return View();
  23.         }
  24.  
  25.         [AcceptVerbs(HttpVerbs.Post)]
  26.         public ActionResult Index(EmailModel emailModel)
  27.         {
  28.             if (ModelState.IsValid)
  29.             {
  30.                 bool isOk = false;
  31.                 try
  32.                 {
  33.                     MailMessage msg = new MailMessage();
  34.                     msg.From = new MailAddress("no-reply@openskymedia.com", "Website Contact Form");
  35.                     msg.To.Add("admin@openskymedia.com");
  36.                     msg.Subject = emailModel.Subject;
  37.                     string body = "Name: " + emailModel.Name + "\n"
  38.                                 + "Email: " + emailModel.EmailAddress + "\n"
  39.                                 + "Website: " + emailModel.WebSite + "\n"
  40.                                 + "Phone: " + emailModel.Phone + "\n\n"
  41.                                 + emailModel.Message;
  42.  
  43.                     msg.Body = body;
  44.                     msg.IsBodyHtml = false;
  45.  
  46.                     SmtpClient smtp = new SmtpClient("smtp.secureserver.net", 80);
  47.                     NetworkCredential Credentials = new NetworkCredential("postmaster2@domain.com", "password");
  48.                     smtp.Credentials = Credentials;
  49.  
  50.                     smtp.Send(msg);
  51.  
  52.                     msg.Dispose();
  53.  
  54.                     isOk = true;
  55.  
  56.                     MessageModel rcpt = new MessageModel();
  57.                     rcpt.Title = "Thank You";
  58.                     rcpt.Content = "Your email has been sent.";
  59.                     return View("Message", rcpt);
  60.                 }
  61.                 catch (Exception ex)
  62.                 {
  63.                 }
  64.  
  65.                 // If we are here...something kicked us into the exception.
  66.                 //
  67.                 MessageModel err = new MessageModel();
  68.                 err.Title = "Email Error";
  69.                 err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
  70.                 return View("Message", err);
  71.             }
  72.             else
  73.             {
  74.                 return View();
  75.             }
  76.         }
  77.  
  78.  
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement