Send mail using ASP.NET

This code shows how to send Email from ASP.NET using your Gmail account

send Email from ASP.NET using your Gmail account

Fist you prepair the application mail.aspx.

like this:

< body>
< form id="form1" runat="server">
< div>
< div style="width: 408px; height: 242px">
< strong> To:</strong>< asp:TextBox ID="ToText" runat="server" Width="263px"></asp:TextBox>
<br />
< strong> From:</strong>
< asp:TextBox ID="FromText" runat="server" Width="256px">< /asp:TextBox>
< strong> Subject:</strong>
< asp:TextBox ID="SubjectText" runat="server" Width="244px" >< /asp:TextBox> <br />
< strong> Message:</strong>
< asp:TextBox ID="MessageText" runat="server" Height="106px" Width="310px">< /asp:TextBox> <br />
<br />
< asp:Button ID="Buttsend" runat="server" Text="Send" Width="77px" OnClick="Buttsend_Click" />
<asp:Label ID="Label2" runat="server"> < asp:Button ID="Buttcancel" runat="server" OnClick="Buttcancel_Click" Text="Cancel" >
< /asp:Button>
< asp:Label ID="Label1" runat="server"> < /div>
< /div>
< /form>
< /body>

and mail.aspx.cs
copy this bellow program..
and you must give bellow program your gmail username and password..
i mentioned in comment tags in program...

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void Buttsend_Click(object sender, EventArgs e)
MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient();
mm.From = new MailAddress(FromText.Text);
mm.To.Add(new MailAddress(ToText.Text));
mm.Subject = SubjectText.Text;
mm.Body = MessageText.Text;
mm.IsBodyHtml = true;
smtp.Host = "";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "your";//write your gmail user name
NetworkCred.Password = "your password";// write pass word smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //Gmail port for e-mail 465 or 587
Label2.Text = "succecc fully send";

protected void Buttcancel_Click(object sender, EventArgs e)
Label1.Text = "try again";


Related Articles

Read Mails from ASP.NET

This article Illustrate How to read mails from ASP.Net. Using POP commands you can access you email inbox from ASP.Net. Basic POP commands are USER, PASS, LIST, QUIT, RETR.

More articles: Sending Email Email using c# Email Gmail account Email ASP.Net


Author: Sudhir19 May 2010 Member Level: Gold   Points : 1


I had used same code after execution i am getting this error:
No connection could be made because the target machine actively refused it

can you help me regarding this..

Author: Naveen Kumar19 May 2010 Member Level: Gold   Points : 1

can you give your correct email address and pass word in the aspx.cs page...?

Author: Sudhir19 May 2010 Member Level: Gold   Points : 0

password= ********

is this right??

Author: Naveen Kumar20 May 2010 Member Level: Gold   Points : 1

you must give your personal gmail account details..
it must be correct user name and password

Naveen kumar

Author: Sudhir20 May 2010 Member Level: Gold   Points : 1

Hi Naveen Kumar,

I got your point, i had given my valid username and password.
same thing i can't write it here:)
so wrote some example...

:) Happy coding.

Guest Author: venkat29 Feb 2012

thank you very much......

Guest Author: sonu15 Jan 2013

i got this error during exicution plz give solution The remote name could not be resolved: ''

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: