use this codes
put this one in the .aspx page :
<form id="form" method="post" runat="server">
<asp:Label ID="Label1" runat="server">Your Name:</asp:Label><br />
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server"> Phone:</asp:Label><br />
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server">Email:</asp:Label><br />
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Label ID="Label4" runat="server">Message:</asp:Label><br />
<textarea runat="server" id="txtContent" style="width: 400px; height: 125px" rows="7"
cols="24"></textarea><br />
<br />
<asp:Button ID="btnSend" Text="Send" OnClick="btnSend_Click" runat="server"></asp:Button><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</form>
<asp:Label ID="Label1" runat="server">Your Name:</asp:Label><br />
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server"> Phone:</asp:Label><br />
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server">Email:</asp:Label><br />
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
<asp:Label ID="Label4" runat="server">Message:</asp:Label><br />
<textarea runat="server" id="txtContent" style="width: 400px; height: 125px" rows="7"
cols="24"></textarea><br />
<br />
<asp:Button ID="btnSend" Text="Send" OnClick="btnSend_Click" runat="server"></asp:Button><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</form>
Put this in the .cs pages
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
string from = "**account here**"; //Replace this with your own correct Gmail Address
string to = "** send to email here *** "; //Replace this with the Email Address to whom you want to send the mail
MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);// new MailAddress(from, txtFrom.Text, System.Text.Encoding.UTF8);
mail.Subject = "Contact by Web";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "Name: " + txtFrom.Text + "<br />Email: " + txtEmail.Text + "<br />Phone: " + txtPhone.Text + "<br /><br />Message:<br />" + txtContent.InnerText;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "***password here****");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage);
} // end try
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
string from = "**account here**"; //Replace this with your own correct Gmail Address
string to = "** send to email here *** "; //Replace this with the Email Address to whom you want to send the mail
MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);// new MailAddress(from, txtFrom.Text, System.Text.Encoding.UTF8);
mail.Subject = "Contact by Web";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "Name: " + txtFrom.Text + "<br />Email: " + txtEmail.Text + "<br />Phone: " + txtPhone.Text + "<br /><br />Message:<br />" + txtContent.InnerText;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "***password here****");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage);
} // end try
{
client.Send(mail);
lblStatus.Text = "Message Sent!";
}
}
}
client.Send(mail);
lblStatus.Text = "Message Sent!";
}
}
}
Comments
Post a Comment