Search code examples
asp.net-mvcasp.net-mvc-3asp.net-mvc-4mvcmailer

MVCMailer sending multiple Messages with no body


I'm using ASP.Net MVC 4 and MVCMailer 4.0 from Nuget. When I create a message and send it to the scaffolded Welcome message everything works fine and I'm able to send the email just fine (the only thing is that its in plain text where as I have html in it)

My problem comes when I use the Contact form to send a message. First let me show you what I have and how I'm using it:

  /*UserMailer.cs*/

  public virtual MvcMailMessage ContactForm(MailMessage mailmessage)
    {
        ViewBag.Name = mailmessage.Name;
        ViewBag.Body = mailmessage.MessageBody;

        return Populate(x =>
        {
            x.Subject = "Scheduler) " + mailmessage.Subject;
            x.ViewName = "Contact Form";
            x.To.Add("Hiva@Varyan.com");
        });
    }

Now on to the View

   @*ContactForm.cshtml*@
   <h2>ContactForm</h2>

   <strong>Name: </strong> @ViewBag.Name <br />
   <strong>Message: </strong> @ViewBag.Body

And Lastly the Contact Controller actions that instantiates the mailer and sends the mail

   //ContactController.cs

   namespace Scheduler.Controllers
   {
       public class ContactController : Controller
       {
           //
           // GET: /Contact/
           private IUserMailer _userMailer = new UserMailer();

           public IUserMailer UserMailer
           {
               get { return _userMailer; }
               set { _userMailer = value; }
           }

           public ActionResult Index()
           {
               return View();
           }

           [HttpPost]
           [ValidateAntiForgeryToken]
           public ActionResult Index(MailMessage _mailMessage)
           {
               UserMailer.ContactForm(_mailMessage).Send();
               return View();
           }

       }
   }

Just for Completeness I'll include my model as well

   //MailMessage.cs
   namespace Scheduler.Model
   {
       public class MailMessage
       {
               public string Name { get; set; }
               public string EmailAddress { get; set; }
               public string Subject { get; set; }
               public string MessageBody { get; set; }
       }
   }

ok, so there are multiple issues that I'm having:

  1. This code somehow generates 2-3 copies of the email and just in case I was even very careful of clicking the submit button on the form.
  2. All the copies of the messages, contain the right From Email address, subject but it does not have any body at all (the messages actually show as "This message has no content" on my phone)
  3. Lastly, when I did have it working (when I first implemented it, and I can't for the life of me see what I did for it to stop working) it sends all messages as plain text and not html.

I've just started learning ASP.Net MVC and if there are any pointers on how to implement the above correctly would be greatly appreciated.


Solution

  • I'm sorry, I've mistakenly placed a space in the x.ViewName and since it was really late didn't catch it.