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("Thanks", Index);
}
else
{
return View();
}
}
}
}
Also here is my View:
@model SteamKing.Models.Contact_Us
@{
ViewBag.Title = "Contact_Us";
}
<h2>Contact_Us</h2>
<fieldset>
<legend>Send Steam King a Message!</legend>
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<p>First Name:</p>
<p>@Html.TextBoxFor(m => m.firstName)</p>
<p>Last Name:</p>
<p>@Html.TextBoxFor(m => m.lastName)</p>
<p>Phone Number:</p>
<p>@Html.TextBoxFor(m => m.phoneNumber)</p>
<p>Email:</p>
<p>@Html.TextBoxFor(m => m.email)</p>
<p>Are you a previous customer:</p>
@Html.DropDownListFor(x => 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")
<p>Message:</p>
<p>@Html.TextAreaFor(m => m.message, new { @cols = 80, @rows = 10 })</p>
<input type ="submit" value="Submit" />
}
</fieldset>
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
Based on the previous post, in your controller you would have to do something like this:
[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("sender@foo.bar.com");
message.To.Add(new MailAddress(model.email));
message.Subject = "New Message from Website";
message.Body = model.message;
SmtpClient client = new SmtpClient();
client.Send(message);
return View("Thanks", Index);
}
else
{
return View();
}
}
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("server@steamkingal.com");
message.To.Add(new MailAddress("services@steamkingal.com"));
message.Subject = "This is my subject";
message.Body = Model.message;
SmtpClient client = new SmtpClient();
client.Send(message);
return View("Thanks", Index);
}
else
{
return View();
}
}
}
}
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...
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:
[HttpPost]
public ViewResult Index(Contact_Us Index) // Your model is passed in here
{
if (ModelState.IsValid)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("server@steamkingal.com");
message.To.Add(new MailAddress("services@steamkingal.com"));
message.Subject = "This is my subject";
// 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("Thanks", Index);
}
else
{
return View();
}
}
repeater09
Member
12 Points
20 Posts
Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 01:20 AM|LINK
Hey guys!
I have been using ASP.Net MVC 3 for about a month now and I gotta say I am in love (Coming from Rails)
I have come to a dead end as of now.
A Client of mine is wanting to have a contact form sent to his email after the user sends the contact form.
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
I havent seen much information about setting up this option in MVC3.
the only thing I have seen is that I need to use the System.Net.Mail.
Is there any tuts or information I can use to get this going?
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
Thanks!
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 01:31 AM|LINK
You can send an email in the controller action method using any .net method for sending email like this
http://www.aspheute.com/english/20000918.asp
Also, take a look at this
http://weblogs.asp.net/gunnarpeipman/archive/2010/10/20/asp-net-mvc-3-beta-using-webmail-helper-to-send-e-mail.aspx
Blog | Twitter : @Hattan
repeater09
Member
12 Points
20 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 02:02 AM|LINK
None of this seems to work.
They are both using System.Web.Mail
I need to know how to use the new System.Net.Mail
Here is my controller if this my help:
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("Thanks", Index); } else { return View(); } } } }Also here is my View:
@model SteamKing.Models.Contact_Us @{ ViewBag.Title = "Contact_Us"; } <h2>Contact_Us</h2> <fieldset> <legend>Send Steam King a Message!</legend> @using (Html.BeginForm()) { @Html.ValidationSummary() <p>First Name:</p> <p>@Html.TextBoxFor(m => m.firstName)</p> <p>Last Name:</p> <p>@Html.TextBoxFor(m => m.lastName)</p> <p>Phone Number:</p> <p>@Html.TextBoxFor(m => m.phoneNumber)</p> <p>Email:</p> <p>@Html.TextBoxFor(m => m.email)</p> <p>Are you a previous customer:</p> @Html.DropDownListFor(x => 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") <p>Message:</p> <p>@Html.TextAreaFor(m => m.message, new { @cols = 80, @rows = 10 })</p> <input type ="submit" value="Submit" /> } </fieldset>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
ignatandrei
All-Star
135142 Points
21676 Posts
Moderator
MVP
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 06:28 AM|LINK
http://www.systemnetmail.com/
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 02:55 PM|LINK
Take a look at this:
Sending Email with System.Net.Mail
Edit: Also, System.Net.Mail isn't new, it's been around for a while.
Blog | Twitter : @Hattan
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 03:01 PM|LINK
Based on the previous post, in your controller you would have to do something like this:
[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("sender@foo.bar.com"); message.To.Add(new MailAddress(model.email)); message.Subject = "New Message from Website"; message.Body = model.message; SmtpClient client = new SmtpClient(); client.Send(message); return View("Thanks", Index); } else { return View(); } }Blog | Twitter : @Hattan
repeater09
Member
12 Points
20 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 10:42 PM|LINK
That does not work (i already used that tut)
problem is that i cannot use the model names i set up
it gives me a error...Do I have to include anything?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 10:47 PM|LINK
What error do you get?
Did you include a using statement for System.Net.Mail at the top of your file?
Blog | Twitter : @Hattan
repeater09
Member
12 Points
20 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 27, 2012 11:46 PM|LINK
Yes! I did
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("server@steamkingal.com"); message.To.Add(new MailAddress("services@steamkingal.com")); message.Subject = "This is my subject"; message.Body = Model.message; SmtpClient client = new SmtpClient(); client.Send(message); return View("Thanks", Index); } else { return View(); } } } }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...
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Asp.Net MVC 3 Send Razor contact form to email
Apr 28, 2012 04:50 AM|LINK
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:
[HttpPost] public ViewResult Index(Contact_Us Index) // Your model is passed in here { if (ModelState.IsValid) { MailMessage message = new MailMessage(); message.From = new MailAddress("server@steamkingal.com"); message.To.Add(new MailAddress("services@steamkingal.com")); message.Subject = "This is my subject"; // 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("Thanks", Index); } else { return View(); } }Blog | Twitter : @Hattan