Asp.Net MVC 3 Send Razor contact form to email http://forums.asp.net/t/1797516.aspx/1?Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Sat, 28 Apr 2012 21:04:13 -040017975164953952http://forums.asp.net/p/1797516/4953952.aspx/1?Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Asp.Net MVC 3 Send Razor contact form to email <p>Hey guys!</p> <p></p> <p>I have been using ASP.Net MVC 3 for about a month now and I gotta say I am in love (Coming from Rails)&nbsp;</p> <p></p> <p>I have come to a dead end as of now.</p> <p></p> <p>A Client of mine is wanting to have a contact form sent to his email after the user sends the contact form.</p> <p></p> <p>this is very easy in Rails and even easier i PHP, but i am doing this Clients website in MVC 3 as I can take as long as I need to complete it</p> <p>I havent seen much information about setting up this option in MVC3.</p> <p>the only thing I have seen is that I need to use the System.Net.Mail.</p> <p></p> <p>Is there any tuts or information I can use to get this going?</p> <p>The form is completed i just need to know what needs to go into the Controller for this form to be sent to my Clients email address</p> <p>Thanks!</p> 2012-04-27T01:20:29-04:004953960http://forums.asp.net/p/1797516/4953960.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>You can send an email in the controller action method using any .net method for sending email like this<br> <a href="http://www.aspheute.com/english/20000918.asp">http://www.aspheute.com/english/20000918.asp</a>&nbsp;</p> <p>Also, take a look at this<br> <a href="http://weblogs.asp.net/gunnarpeipman/archive/2010/10/20/asp-net-mvc-3-beta-using-webmail-helper-to-send-e-mail.aspx">http://weblogs.asp.net/gunnarpeipman/archive/2010/10/20/asp-net-mvc-3-beta-using-webmail-helper-to-send-e-mail.aspx</a>&nbsp;</p> <p></p> <p></p> 2012-04-27T01:31:10-04:004953975http://forums.asp.net/p/1797516/4953975.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>None of this seems to work.&nbsp;</p> <p></p> <p>They are both using System.Web.Mail</p> <p>I need to know how to use the new System.Net.Mail</p> <p>Here is my controller if this my help:</p> <p></p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SteamKing.Models; using System.Net.Mail; namespace SteamKing.Controllers { public class Contact_UsController : Controller { // // GET: /Contact_Us/ [HttpGet] public ViewResult Index() { return View(); } [HttpPost] public ViewResult Index(Contact_Us Index) { if (ModelState.IsValid) { //I need the Email sender to go here. return View(&quot;Thanks&quot;, Index); } else { return View(); } } } }</pre> <p></p> <p>Also here is my View:</p> <pre class="prettyprint">@model SteamKing.Models.Contact_Us @{ ViewBag.Title = "Contact_Us"; } &lt;h2&gt;Contact_Us&lt;/h2&gt; &lt;fieldset&gt; &lt;legend&gt;Send Steam King a Message!&lt;/legend&gt; @using (Html.BeginForm()) { @Html.ValidationSummary() &lt;p&gt;First Name:&lt;/p&gt; &lt;p&gt;@Html.TextBoxFor(m =&gt; m.firstName)&lt;/p&gt; &lt;p&gt;Last Name:&lt;/p&gt; &lt;p&gt;@Html.TextBoxFor(m =&gt; m.lastName)&lt;/p&gt; &lt;p&gt;Phone Number:&lt;/p&gt; &lt;p&gt;@Html.TextBoxFor(m =&gt; m.phoneNumber)&lt;/p&gt; &lt;p&gt;Email:&lt;/p&gt; &lt;p&gt;@Html.TextBoxFor(m =&gt; m.email)&lt;/p&gt; &lt;p&gt;Are you a previous customer:&lt;/p&gt; @Html.DropDownListFor(x =&gt; x.previousCustomer, new[] { new SelectListItem() {Text = "Yes, I am", Value = bool.TrueString}, new SelectListItem() {Text = "No, I am not", Value = bool.FalseString} }, "Choose an option") &lt;p&gt;Message:&lt;/p&gt; &lt;p&gt;@Html.TextAreaFor(m =&gt; m.message, new { @cols = 80, @rows = 10 })&lt;/p&gt; &lt;input type ="submit" value="Submit" /&gt; } &lt;/fieldset&gt; </pre> <p>so i need to be able to send that form to the Clients email address letting them know who sent it and what was said in the message<br> <br> </p> <p></p> 2012-04-27T02:02:06-04:004954223http://forums.asp.net/p/1797516/4954223.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p></p> <blockquote><span class="icon-blockquote"></span> <h4>repeater09</h4> I need to know how to use the new System.Net.Mail</blockquote> <p></p> <p>http://www.systemnetmail.com/</p> <p></p> 2012-04-27T06:28:55-04:004955155http://forums.asp.net/p/1797516/4955155.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>Take a look at this:<br> <a href="http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx">Sending Email with System.Net.Mail&nbsp;</a></p> <p>Edit: Also, System.Net.Mail isn't new, it's been around for a while.<br> <br> </p> 2012-04-27T14:55:46-04:004955166http://forums.asp.net/p/1797516/4955166.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>Based on the previous post, in &nbsp;your controller you would have to do something like this:</p> <pre class="prettyprint">[HttpPost] public ViewResult Index(Contact_Us model) { if (ModelState.IsValid) { //I need the Email sender to go here. MailMessage message = new MailMessage(); message.From = new MailAddress(&quot;sender@foo.bar.com&quot;); message.To.Add(new MailAddress(model.email)); message.Subject = &quot;New Message from Website&quot;; message.Body = model.message; SmtpClient client = new SmtpClient(); client.Send(message); return View(&quot;Thanks&quot;, Index); } else { return View(); } }</pre> <p><br> &nbsp;</p> 2012-04-27T15:01:02-04:004955601http://forums.asp.net/p/1797516/4955601.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>That does not work (i already used that tut)</p> <p>problem is that i cannot use the model names i set up&nbsp;</p> <pre class="prettyprint">message.Body = model.message;</pre> <p>it gives me a error...Do I have to include anything?</p> <p></p> 2012-04-27T22:42:24-04:004955604http://forums.asp.net/p/1797516/4955604.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>What error do you get?</p> <p>Did you include a using statement for System.Net.Mail at the top of your file?&nbsp;</p> <pre class="prettyprint">using System.Net.Mail;</pre> <p><br> &nbsp;</p> 2012-04-27T22:47:22-04:004955622http://forums.asp.net/p/1797516/4955622.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>Yes! I did&nbsp;</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SteamKing.Models; using System.Net.Mail; namespace SteamKing.Controllers { public class Contact_UsController : Controller { // // GET: /Contact_Us/ [HttpGet] public ViewResult Index() { return View(); } [HttpPost] public ViewResult Index(Contact_Us Index) { if (ModelState.IsValid) { MailMessage message = new MailMessage(); message.From = new MailAddress(&quot;server@steamkingal.com&quot;); message.To.Add(new MailAddress(&quot;services@steamkingal.com&quot;)); message.Subject = &quot;This is my subject&quot;; message.Body = Model.message; SmtpClient client = new SmtpClient(); client.Send(message); return View(&quot;Thanks&quot;, Index); } else { return View(); } } } }</pre> <p>I am sorry if i am being hard on this i have been able to use the mail feature but not send what someone puts in the form to email that is what i am having trouble doing...</p> 2012-04-27T23:46:21-04:004955731http://forums.asp.net/p/1797516/4955731.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>No problem. It looks like the issue is that you are referencing a variable called Model. However, in your case the model being passed in as input parameter is called Index. The parameter that gets passed into the action method is the model that gets binded, so you need to change your action method to this:<br> &nbsp;</p> <pre class="prettyprint">[HttpPost] public ViewResult Index(Contact_Us Index) // Your model is passed in here { if (ModelState.IsValid) { MailMessage message = new MailMessage(); message.From = new MailAddress(&quot;server@steamkingal.com&quot;); message.To.Add(new MailAddress(&quot;services@steamkingal.com&quot;)); message.Subject = &quot;This is my subject&quot;; // You need to use Index because that is the name declared above message.Body = Index.message; SmtpClient client = new SmtpClient(); client.Send(message); return View(&quot;Thanks&quot;, Index); } else { return View(); } }</pre> <p></p> 2012-04-28T04:50:45-04:004956257http://forums.asp.net/p/1797516/4956257.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>Ok it seemed to have Zero Errors which is awesome! but now mwhen i try to access Contact_Us/index it comes with a 404 error. it worked fine(Not the email part but the formb did show correctly) before that but when i put that code in there it no longer works.</p> <p></p> <p></p> 2012-04-28T14:06:47-04:004956449http://forums.asp.net/p/1797516/4956449.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>OK! i freaking got it to work.</p> <p></p> <p>here is what I did&nbsp;</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SteamKing.Models; using System.Net.Mail; namespace SteamKing.Controllers { public class Contact_UsController : Controller { // // GET: /Contact_Us/ public ViewResult index() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ViewResult index(Contact_Us index) // Your model is passed in here { if (ModelState.IsValid) { MailMessage message = new MailMessage(); message.From = new MailAddress(&quot;server@steamkingal.com&quot;); message.To.Add(new MailAddress(&quot;services@steamkingal.com&quot;)); message.Subject = index.email; // You need to use Index because that is the name declared above message.Body = index.message; SmtpClient client = new SmtpClient(); client.Send(message); return View(&quot;Thanks&quot;, index); } else { return View(); } } } }</pre> <p>Now, my last questions is how do i pass more then one model in the message itself?</p> <p></p> <pre class="prettyprint"> // You need to use Index because that is the name declared above message.Body = index.message; </pre> <p>Gives me a error is i try to pass another model into it,...thats the last thing i need to knowww!<br> <br> <br> </p> 2012-04-28T20:51:08-04:004956452http://forums.asp.net/p/1797516/4956452.aspx/1?Re+Asp+Net+MVC+3+Send+Razor+contact+form+to+email+Re: Asp.Net MVC 3 Send Razor contact form to email <p>What do you mean pass more than one model to the message itself? Can you elaborate on that?</p> <p>The message.Body is just a string so you can user a string builder or concatenate strings to body. For example:</p> <pre class="prettyprint">message.Body = index.previousCustomer &#43; &quot; &quot; &#43; index.message;</pre> <p><br> <br> </p> 2012-04-28T21:04:13-04:00