image

I get TONS of email from customer.

Over time, this email helps me to determine what areas in our product collection are opportunities for enhancement or improvement.

I store the email that comes from my blog contact form in folders and then search through them looking for trends periodically.

It occurred to me that, while I need to get the emails because many of them are actionable, it would be great if I could use reporting and analysis tools against the collection.

So I whipped together a sample in ASP.NET MVC

I set up a simple SQL Express database and use Visual Studio to generate an EntityDataModel for it.

I implemented the view above and an ActionResult for HttpPost events from the ContactUs view.

Code Snippet
  1. [HttpPost]
  2. public ActionResult ContactUs(Message msg)
  3. {
  4.     if (ModelState.IsValid)
  5.     {
  6.         // Same the Message in the Massages Table.
  7.         try
  8.         {
  9.            using(ContactMailEntities db = new ContactMailEntities())
  10.            {
  11.                db.AddToMessages(msg);
  12.                db.SaveChanges();                  
  13.            }
  14.         }
  15.         catch
  16.         {
  17.             // Store Failed.
  18.             return View(msg);
  19.         }
  20.  
  21.         // Send the Message in email.
  22.         if (!SendMailMessage(msg))
  23.         {
  24.             // Send Failed.
  25.             return View(msg);
  26.         }
  27.     }
  28.     else
  29.     {
  30.         return View(msg);
  31.     }
  32.  
  33.     // Success
  34.    return View("MessageSent");
  35. }

Since my view uses that model, on post back my ActioResult can just create an instance of the model and call the generated “AddToMessages” (to insert to the Messages Table)  method and pass in the Message instance from the View.

Wow – easy and elegant.

BTW the SendMailMessage method looks like this.

Code Snippet
  1. public bool SendMailMessage(Message msg)
  2. {
  3.     try
  4.     {
  5.         MailMessage mailMsg = new MailMessage();
  6.         mailMsg.To.Add("Joe@Stagner.net");
  7.         MailAddress mailAddress = new MailAddress(msg.FromEmail, msg.FromName);
  8.         mailMsg.From = mailAddress;
  9.         mailMsg.Subject = msg.Subject;
  10.         mailMsg.Body = msg.ContactMessage;
  11.  
  12.         SmtpClient smtpClient = new SmtpClient("mail.stagner.net", 2525);
  13.         System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("Joe@Stagner.net", "bingo117");
  14.         smtpClient.Credentials = credentials;
  15.  
  16.         smtpClient.Send(mailMsg);
  17.         return true;
  18.     }
  19.     catch (Exception ex)
  20.     {
  21.         return (false);
  22.     }
  23.  
  24. }

Hope someone finds it useful.

[ Full project HERE ]