How to send mail through gmail on ASP.NET
This article is used to send mail to client using smtp.gmail.com host in ASP.NET .we can send a simple mail through our gmail acount to anyone . you can embed this code snippet in your application to send mail through gmail account .
<b> How to send mail grom gmail to client <b>
Here is code
1) Create one Visual studio project name as SendmailApplication .
2) open you default.aspx (designer page). and write down below code
Default.aspx page :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID ="Sender" runat="server" AssociatedControlID="txtSender" Text="From :" Width="600"></asp:Label>
<asp:TextBox ID="txtSender" runat ="server" Width="200px"
style="position: relative; top: 0px; left: 90px" ></asp:TextBox><br />
<br />
<asp:Label ID="receiver" runat="server" AssociatedControlID="txtreceiver" Text="To :"></asp:Label>
<asp:TextBox ID ="txtreceiver" runat="server"
style="position: relative; top: 0px; left: 110px; width: 214px"></asp:TextBox><br />
<br />
<asp:Label ID="subject" runat="server" AssociatedControlID="txtsubject" Text="Subject :"></asp:Label>
<br />
<asp:Label ID="msgbody" runat="server" AssociatedControlID="txtmsgbody"
Text="Body :" style="position: relative"></asp:Label>
<asp:TextBox ID ="txtmsgbody" runat="server" Height="93px" TextMode="MultiLine"
style="position: relative; top: 0px; left: 80px; width: 484px"></asp:TextBox><br />
<br />
<asp:Button ID="btnsend" runat="server" Text="Send" onclick="btnsend_Click"
style="position: relative; top: 0px; left: 450px" />
</div>
</form>
</body>
</html>
3) Add Namespace to send mail
Using System.Net;
Using System.Net.Mail;
4) Create Instance For MailMessage - used to create message format and body.
paramenter - Sender and receiver ( from,To)
5) Assign body and subject to mailmessage instance .
6) Create instance of SmtpClient assign host - smtp.gmail.com
and port - 587
7) Provide credential of your domain and send mail .
Default.aspx.cs page :
protected void btnsend_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage(new MailAddress(txtSender.Text), new MailAddress(txtreceiver.Text));
mail.Subject = txtsubject.Text;
mail.Body = txtmsgbody.Text;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("sagar******@gmail.com", "********");
client.EnableSsl = true;
client.Send(mail);
}
catch (Exception ex)
{
}
}
please post on resource for more details
Thanks
Sagar Pawar
Hi im getting "Failure sending mail." How to resolve this?
Plz explain.