Introduction:In this article you are going to learn how to send an Email in Asp.net using Gmail.
Step 1:Create a new Empty Web Application using Visual Studio.
Step 2:Right Click Application name in Server Explorer then Select Add then New Item.
Step 3:Select WebForm from the list of Templates and named it Email and Click Add.
Step 4: Now Open Email.aspx and copy below Code in the Form Tag.
<center>
<div>
<table width="50%">
<tr>
<td style="width:2%;">
</td>
<td style="width:30%;">
</td>
</tr>
<tr>
<td>
</td>
<td>
<h2>How to send an Email in Aps.net using Gmail</h2>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTo" runat="server" Text="To"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_To" runat="server" ErrorMessage="*" ControlToValidate="txtTo" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessageTitle" runat="server" Text="Title"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_Title" runat="server" ErrorMessage="*" ControlToValidate="txtTitle" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessageBody" runat="server" Text="Body"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="15" Rows="5"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_Body" runat="server" ErrorMessage="*" ControlToValidate="txtBody" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset" CausesValidation="false"
onclick="btnReset_Click" />
</td>
</tr>
</table>
</div>
</center>
Step 5: Now open Email.aspx Code behind file by pressing f7 and copy below code in your Email Class.
Note: First You need to import below two namespaces in order to use mailmessage and networkCredentials classes in your application.
using System.Net;
using System.Net.Mail;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Reset();
txtTo.Focus();
}
}
private void Reset()
{
lblMessage.Text = "";
txtTo.Text = "";
txtTitle.Text = "";
txtBody.Text = "";
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (IsValid)
{
SendMail();
}
}
private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
mail.From = new MailAddress("Your Gmail Id", "Your Name");
mail.To.Add(txtTo.Text);
mail.Subject = txtTitle.Text;
mail.Body = txtBody.Text;
client.Credentials = new NetworkCredential("Your Gmail ID", "Your Gmail Password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
Reset();
lblMessage.Text = "Mail Sent.";
}
protected void btnReset_Click(object sender, EventArgs e)
{
Reset();
}
Enjoy Sending Emails.
Happy Coding!!
Step 1:Create a new Empty Web Application using Visual Studio.
Step 2:Right Click Application name in Server Explorer then Select Add then New Item.
Step 3:Select WebForm from the list of Templates and named it Email and Click Add.
Step 4: Now Open Email.aspx and copy below Code in the Form Tag.
<center>
<div>
<table width="50%">
<tr>
<td style="width:2%;">
</td>
<td style="width:30%;">
</td>
</tr>
<tr>
<td>
</td>
<td>
<h2>How to send an Email in Aps.net using Gmail</h2>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTo" runat="server" Text="To"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_To" runat="server" ErrorMessage="*" ControlToValidate="txtTo" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessageTitle" runat="server" Text="Title"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_Title" runat="server" ErrorMessage="*" ControlToValidate="txtTitle" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessageBody" runat="server" Text="Body"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="15" Rows="5"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_Body" runat="server" ErrorMessage="*" ControlToValidate="txtBody" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset" CausesValidation="false"
onclick="btnReset_Click" />
</td>
</tr>
</table>
</div>
</center>
Step 5: Now open Email.aspx Code behind file by pressing f7 and copy below code in your Email Class.
Note: First You need to import below two namespaces in order to use mailmessage and networkCredentials classes in your application.
using System.Net;
using System.Net.Mail;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Reset();
txtTo.Focus();
}
}
private void Reset()
{
lblMessage.Text = "";
txtTo.Text = "";
txtTitle.Text = "";
txtBody.Text = "";
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (IsValid)
{
SendMail();
}
}
private void SendMail()
{
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
mail.From = new MailAddress("Your Gmail Id", "Your Name");
mail.To.Add(txtTo.Text);
mail.Subject = txtTitle.Text;
mail.Body = txtBody.Text;
client.Credentials = new NetworkCredential("Your Gmail ID", "Your Gmail Password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
Reset();
lblMessage.Text = "Mail Sent.";
}
protected void btnReset_Click(object sender, EventArgs e)
{
Reset();
}
Enjoy Sending Emails.
Happy Coding!!
No comments:
Post a Comment