How to send E-mail using ASP.net through Gmail account

Sending Email is a basic task of a web application for many purposes . If you have any web space somewhere on a web server then you are provided with the Email accounts and SMTP and POP services for sending and receiving E-mails.
If you want to send email using your Gmail account or using Gmail's smtp server in ASP.NET application or if you don't have a working smtp server to send mails using your ASP.NET application or aspx page then sending e-mail using Gmail is best option.
But Gmail allow to send mail to limited number of people per day and you can’t send same email to unlimited number of people Gmail suppose it as spam message.
So if you want to send mail to limited number of people from asp.net page then sending mail through gmail will be best option.
Here I am showing the step by step process .

1-add a new web application in visual studio and add a Web form to it.And add a button on this page.We will use this buttong click event to send Email.

2-Add NameSpace System.Net.Mail by using this
using System.Net.Mail;

3-on the button click event add this code

string Emailfrom = from@gmail.com; //Replace this with your own Gmail id

string Emailto = to@gmail.com //Replace this with the Email Address where you want to send the Email

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(Emailto);
mail.From = new MailAddress(Emailfrom, "hello" , System.Text.Encoding.UTF8);
mail.Subject = "Put your Email subject Here" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "put your message here thet you want to sent";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
//Add the your gmail id and password
client.Credentials = new System.Net.NetworkCredential(Emailfrom, "Password");
client.Port = 587; // this is the port where gmail work
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 );
}

Replae to and from email with the real email otherwise code will not work.
Tags:

Join Us!