** I kno this is a little cheeky, but if you feel a little donation via paypal may help on successfull completion im more than happy to do so!!****
Hi all after eventually mastering, well when i say mastering I really mean getting the answers from these forums on how to from to email in standard asp.net.
I have decided to update my asp.net website to asp.net mvc. I have the following contact form:
<%
IList<string> errors = ViewData["errors"]
as
IList<string>;
if (errors !=
null) {
%>
<ul
class="error">
<% foreach (string error
in errors) { %>
<li><%=
Html.Encode(error) %></li>
Please be aware the javascript is nothing more than a client side captcha script that blocks submit of the form until the correct captcha word is entered.
The "email" and "enquiry" fields are required and I would like them to fire validation,, similar to the standard Login control validates in the mvc sample that is included
on start of new mvc web application.
The form is to be enabled by the HomeController.cs and the controller name is Contact same as the view name, so the controller should only fire when in Post
eg.
// Non-POST requests should just display the ChangePassword form
if (Request.HttpMethod !=
"POST")
{
return View();
}
I have the following standard code for the controller in the HomeController.cs file:
public
ActionResult Contact()
{
ViewData["Title"] =
"Contact";
ViewData[
"Message"] =
"Get Intouch";
return View();
}
Could someone please provide me with the necessary expertise to enable the control.
PS. Do i need to make any alterations to the web.config file also?
I am using mvc preview 5 if that makes any difference.
I have attempted to play around with existing asp.net smtp email code to try and get it to work but to no success.
So if someone could please provide me with the code to for the controller i need to get this working I would be ever so grateful.
I tried the following code, and found out the MVC mechanism worked. However, because my machine is Vista, and by default Vista does not install SMTP service, I don't know if email sending works. You can try it in XP with SMTP service enabled, it should work.
[AcceptVerbs("GET")]
public ActionResult Index()
{
return View();
}
[AcceptVerbs("Post")]
public ActionResult Index(string name, string email, string messenger, string phone, string enquiry)
{
var mailMessage = new System.Net.Mail.MailMessage("test@testdomain.com", email, "Subject: test", enquiry);
var smtp = new System.Net.Mail.SmtpClient("localhost", 25);
smtp.Send(mailMessage);
return View("Success");
}
Assumption:
* The GET verb will display the contact form
* The submit button click will commence POST verb to the same controller.
* upon completion of email sending, it will display the Success view
dhinch81
Member
10 Points
15 Posts
MVC Form to email.
Sep 29, 2008 03:32 PM|LINK
** I kno this is a little cheeky, but if you feel a little donation via paypal may help on successfull completion im more than happy to do so!!****
Hi all after eventually mastering, well when i say mastering I really mean getting the answers from these forums on how to from to email in standard asp.net.
I have decided to update my asp.net website to asp.net mvc. I have the following contact form:
<%
IList<string> errors = ViewData["errors"] as IList<string>; if (errors != null) { %> <ul class="error"> <% foreach (string error in errors) { %> <li><%= Html.Encode(error) %></li><% } %>
</ul><%
}
%>
<form method="post" onsubmit="return jcap();" action="<%= Html.AttributeEncode(Url.Action("Contact")) %>"> <table> <tr> <td class="width">Contact Name</td> <td><%= Html.TextBox("name") %></td> </tr> <tr> <td>Email Address <span>*</span></td> <td><%= Html.TextBox("email") %></td> </tr> <tr> <td>Messenger:</td> <td><%= Html.TextBox("messenger") %></td> </tr> <tr> <td>Telephone:</td> <td><%= Html.TextBox("phone") %></td> </tr> <tr> <td class="top">Enquiry <span>*</span></td> <td><%= Html.TextArea("enquiry") %></td> </tr> <tr> <td>Type Security word <span>*</span></td> <td><script type="text/javascript">sjcap("small");</script></td> </tr> <tr> <td> </td> <td><input type="image" value="Contact" class="button" src="../../Content/Images/Other/btn_contact.gif" /></td> </tr> </table> </form>Please be aware the javascript is nothing more than a client side captcha script that blocks submit of the form until the correct captcha word is entered.
The "email" and "enquiry" fields are required and I would like them to fire validation,, similar to the standard Login control validates in the mvc sample that is included on start of new mvc web application.
The form is to be enabled by the HomeController.cs and the controller name is Contact same as the view name, so the controller should only fire when in Post
eg.// Non-POST requests should just display the ChangePassword form
if (Request.HttpMethod != "POST"){
return View();}
I have the following standard code for the controller in the HomeController.cs file:
public ActionResult Contact(){
ViewData["Title"] = "Contact";ViewData[
"Message"] = "Get Intouch"; return View();}
Could someone please provide me with the necessary expertise to enable the control.
PS. Do i need to make any alterations to the web.config file also?
I am using mvc preview 5 if that makes any difference.
I have attempted to play around with existing asp.net smtp email code to try and get it to work but to no success.
So if someone could please provide me with the code to for the controller i need to get this working I would be ever so grateful.
Thank you in advance.
MVC MVC Preview 5 mvc email smtp
suhanto
Member
295 Points
74 Posts
Re: MVC Form to email.
Sep 30, 2008 09:50 AM|LINK
I tried the following code, and found out the MVC mechanism worked. However, because my machine is Vista, and by default Vista does not install SMTP service, I don't know if email sending works. You can try it in XP with SMTP service enabled, it should work.
[AcceptVerbs("GET")] public ActionResult Index() { return View(); } [AcceptVerbs("Post")] public ActionResult Index(string name, string email, string messenger, string phone, string enquiry) { var mailMessage = new System.Net.Mail.MailMessage("test@testdomain.com", email, "Subject: test", enquiry); var smtp = new System.Net.Mail.SmtpClient("localhost", 25); smtp.Send(mailMessage); return View("Success"); }Assumption:
* The GET verb will display the contact form
* The submit button click will commence POST verb to the same controller.
* upon completion of email sending, it will display the Success view
Regards,
Agus Suhanto
ASP.NET MVC
Agus Suhanto