Sending mail from web application using google Mail ID
Sending mail from web application by using google Mail ID:
In this post we describe how to Send confirmation mail
from using Google mail.id
As we know that for using mail we need to three thing :
1. Google Mail ID.(sender Email.id And Password).
2. Receiver Mail.ID .
3. Smtp(Gmai also give SMTP and port Number) .
In Asp.net it is very Easy task, here we give some segment of code for solving this Question.Here we make a web page in .Net programming with C#. And give code of both file .aspx page and back End .aspc.cs page.
Send_mail.aspx page code :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="inquery_mail.aspx.cs" Inherits="inquery_mail" %>
<!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 id="Head1" runat="server">
<title></title>
<style type="text/css">
#form1
{
height: 244px;
width: 302px;
}
.style1
{
height: 227px;
}
.style2
{
width: 107px;
}
.style3
{
width: 162px;
}
</style>
</head>
<body style="height: 220px; width: 303px">
<form id="form1" runat="server">
<table class="style1"
style="border: thick solid #333333; table-layout: fixed; background-color: #B42908; width: 275px;"
frame="border">
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style2">
Nname :
</td>
<td class="style3">
<asp:TextBox ID="txtUserName" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Mob no :
</td>
<td class="style3">
<asp:TextBox ID="txtUsermob" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Email address :</td>
<td class="style3">
<asp:TextBox ID="txtConfirmEmail" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left" class="style2">
Query :</td>
<td class="style3">
<asp:TextBox ID="txtQuery" runat="server" Height="89px" TextMode="MultiLine"
Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="submit" runat="server" Text="submit" onclick="submit_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
<!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 id="Head1" runat="server">
<title></title>
<style type="text/css">
#form1
{
height: 244px;
width: 302px;
}
.style1
{
height: 227px;
}
.style2
{
width: 107px;
}
.style3
{
width: 162px;
}
</style>
</head>
<body style="height: 220px; width: 303px">
<form id="form1" runat="server">
<table class="style1"
style="border: thick solid #333333; table-layout: fixed; background-color: #B42908; width: 275px;"
frame="border">
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
</tr>
<tr>
<td class="style2">
Nname :
</td>
<td class="style3">
<asp:TextBox ID="txtUserName" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Mob no :
</td>
<td class="style3">
<asp:TextBox ID="txtUsermob" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Email address :</td>
<td class="style3">
<asp:TextBox ID="txtConfirmEmail" runat="server" Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left" class="style2">
Query :</td>
<td class="style3">
<asp:TextBox ID="txtQuery" runat="server" Height="89px" TextMode="MultiLine"
Width="129px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="submit" runat="server" Text="submit" onclick="submit_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
asp.net by parijat mishra |
Send_mail.aspx.cs page 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 inquery_mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
try
{
if(txtConfirmEmail.Text.Trim().Length!=0)
{
SendEmailToClient(txtConfirmEmail.Text.ToString());
}
}
catch (Exception)
{
throw;
}
}
private void SendEmailToClient(string EmailAdd)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
sb2.Append(" <table><tr><td>Name :" + txtUserName.Text + "</td></tr> <tr><td> Phone number :" + txtUsermob.Text + "</td> </tr><tr><td> Email-id :" + txtConfirmEmail.Text + "</td> </tr><tr><td> Query :" + txtQuery.Text + "</td> </tr></table> </div>");
sb1.Append("<table><tr><td>Name :" + txtUserName.Text + "</td></tr> <tr><td> Phone number :" + txtUsermob.Text + "</td> </tr><tr><td> Email-id :" + txtConfirmEmail.Text + "</td> </tr><tr><td> Query :" + txtQuery.Text + "</td> </tr></table> </div>");
MailAddress fromAddress = new MailAddress("<mailAddress>","username");
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
message.From = fromAddress;
message.To.Add(txtConfirmEmail.Text);
message.Subject = "Feedback";
message.Body = txtQuery.Text;
message.IsBodyHtml = false;
smtpClient.Credentials = new System.Net.NetworkCredential("idsender", " sender password");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
}
}
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 inquery_mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
try
{
if(txtConfirmEmail.Text.Trim().Length!=0)
{
SendEmailToClient(txtConfirmEmail.Text.ToString());
}
}
catch (Exception)
{
throw;
}
}
private void SendEmailToClient(string EmailAdd)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
sb2.Append(" <table><tr><td>Name :" + txtUserName.Text + "</td></tr> <tr><td> Phone number :" + txtUsermob.Text + "</td> </tr><tr><td> Email-id :" + txtConfirmEmail.Text + "</td> </tr><tr><td> Query :" + txtQuery.Text + "</td> </tr></table> </div>");
sb1.Append("<table><tr><td>Name :" + txtUserName.Text + "</td></tr> <tr><td> Phone number :" + txtUsermob.Text + "</td> </tr><tr><td> Email-id :" + txtConfirmEmail.Text + "</td> </tr><tr><td> Query :" + txtQuery.Text + "</td> </tr></table> </div>");
MailAddress fromAddress = new MailAddress("<mailAddress>","username");
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
message.From = fromAddress;
message.To.Add(txtConfirmEmail.Text);
message.Subject = "Feedback";
message.Body = txtQuery.Text;
message.IsBodyHtml = false;
smtpClient.Credentials = new System.Net.NetworkCredential("idsender", " sender password");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
}
}