I have a simple contact form with validation that I need to send mail from. I have seen some contact forms that use Gmail to email from a form but I am not sure how to use it with my form. Could someone please show me how to do this? Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
labelMessage.Text = "";
}
protected void buttonSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
labelMessage.Text = "Thank you. Your information has been sent.";
}
}
}
mudassarkhan, Am I Understanding this correctly? Change the "sender@gmail.com and the reciever@gmail.com to my working gmail accounts? And then change the NetworkCred.UserName = "sender@gmail.com"; to my gmail address. And change the password here NetworkCred.Password
= "xxxxx"; ? The reason I ask is because I am not getting any errors when I use the form, but I am not recieving the emails.
ChadMH
Member
9 Points
50 Posts
Send email from form
May 01, 2012 10:38 AM|LINK
I have a simple contact form with validation that I need to send mail from. I have seen some contact forms that use Gmail to email from a form but I am not sure how to use it with my form. Could someone please show me how to do this? Here is my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div id="info" style="width: 383px"> <table style="width: 383px;" id="tableInfo"> <!--row name--> <tr id="rowName"> <td width="70"> Name: </td> <td width="10"> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name is required" ControlToValidate="textName" Display="Dynamic" ValidationGroup="AllValidators">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="textName" runat="server" Width="303px" MaxLength="25"></asp:TextBox> </td> </tr> <!--end row name--> <tr id="rowPhone"> <td width="70"> Phone: </td> <td width="10"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Phone Number Is Required" ControlToValidate="textPhone" Display="Dynamic" ValidationGroup="AllValidators">*</asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="textPhone" runat="server" Width="303px" MaxLength="11"></asp:TextBox> </td> </tr> <!--end row phone--> <tr id="rowEmail"> <td width="70"> Eamil: </td> <td width="10"> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Email Address Is Required" ControlToValidate="textEmail" Display="Dynamic" ValidationGroup="AllValidators">*</asp:RequiredFieldValidator> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="textEmail" Display="Dynamic" ErrorMessage=" E-mail addresses must be in the format of name@domain.xyz." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="AllValidators">Invalid Format!</asp:RegularExpressionValidator> <asp:TextBox ID="textEmail" runat="server" Width="303px" MaxLength="50"></asp:TextBox> </td> </tr> <!--end row email--> <tr id="rowComment"> <td> Comments: </td> <td> </td> <td> <asp:TextBox ID="textComment" runat="server" Rows="5" TextMode="MultiLine" Width="303px"></asp:TextBox> </td> </tr> </table> </div> <asp:Button ID="buttonSubmit" runat="server" onclick="buttonSubmit_Click" Text="Submit" ValidationGroup="AllValidators" /> <br /> <asp:Label ID="labelMessage" runat="server"></asp:Label> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ValidationGroup="AllValidators" /> </form> </body> </html>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { labelMessage.Text = ""; } protected void buttonSubmit_Click(object sender, EventArgs e) { if (Page.IsValid) { labelMessage.Text = "Thank you. Your information has been sent."; } } }Chad
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Send email from form
May 01, 2012 10:41 AM|LINK
refer to this
http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: Send email from form
May 01, 2012 10:42 AM|LINK
System.Net.Mail; //Include This NameSpace MailMessage MyMailMessage = new MailMessage(); MyMailMessage.From = new MailAddress("adbuaryuy@gmail.com"); MyMailMessage.To.Add("yearningpeeks@yahoo.com"); MyMailMessage.Subject = "Feedback Form"; MyMailMessage.IsBodyHtml = true; MyMailMessage.Body = "<table><tr><td>" + txtName.Text + txtEmail.Text + txtComments.Text + "</table></tr></td>"; SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com"); SMTPServer.Port = 587; SMTPServer.Credentials = new System.Net.NetworkCredential("adbuaryuy@gmail.com", System.Configuration.ConfigurationSettings.AppSettings["pwd"].ToString()); SMTPServer.EnableSsl = true; try { SMTPServer.Send(MyMailMessage); Response.Redirect("Thankyou.aspx"); } catch (Exception ex) { }ramiramilu
All-Star
95493 Points
14106 Posts
Re: Send email from form
May 01, 2012 11:38 AM|LINK
if you can closely look at this tutorial you have everything to send email from gmail using contact form with validations too - http://www.intstrings.com/ramivemula/asp-net/ajax-html-editor-based-contact-form-inside-updatepanel-along-with-updateprogress/
just put that email code in your button click...
Thanks,
JumpStart
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Send email from form
May 01, 2012 12:02 PM|LINK
Compete code available here
http://www.aspsnippets.com/Articles/Contact-Us-Form-with-Rich-TextBox-in-ASP.Net.aspx
Contact me
ChadMH
Member
9 Points
50 Posts
Re: Send email from form
May 01, 2012 12:37 PM|LINK
mudassarkhan, Am I Understanding this correctly? Change the "sender@gmail.com and the reciever@gmail.com to my working gmail accounts? And then change the NetworkCred.UserName = "sender@gmail.com"; to my gmail address. And change the password here NetworkCred.Password = "xxxxx"; ? The reason I ask is because I am not getting any errors when I use the form, but I am not recieving the emails.
Chad
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: Send email from form
May 01, 2012 12:43 PM|LINK
Change these values and fill in the form.
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "xxxxx";
ChadMH
Member
9 Points
50 Posts
Re: Send email from form
May 01, 2012 10:44 PM|LINK
Ok, I got it to work on my local machine but it times out on the server. Any Ideas what to do?
Chad