Send Email from your Gmail Account using ASP.NET


In this article I have explained how to send email using ASP.NET with your GMAIL account. We can use system.net.mail and smtp pop server for sending email using asp.net. This article help you to configure your code and some userful settings.

How to send Email from your Gmail Account using ASP.NET


introduction
Many times I have seen on our dotnetspider website users put their question related to email sending using ASP.NET. This is a small attempt to help all developers who unaware of email sending using ASP.NET

Things Required before building code to send emails


We should keep all mandatory tools in our toolbox before building code, here is list of required things

1. Receiver Email address
2. Sender Email: UserID and Password
3. Namespace System.Net.Mail
4. Email Attachment if any
5. Last but not least you should keep enable your mail for POP settings in your Gmail account, following screen shot will clear idea.

POP



Getting started
After collecting all required tool let's starts with the website creation

1. create a website in visual studio
new_website



----------------------------------------------


2. Design form as per requirement, i have design a simple form here you can check

design_form



----------------------------------------------

3. put following code in your code behind, on send email click in TRY...CATCH block



MailMessage mail = new MailMessage();

//put mail from Email
mail.From = new MailAddress("MailFrom@gmail.com");

//put mail to Email
mail.To.Add(new MailAddress("MailTo@gmail.com"));


//you can add blank carbon copy mails here
//mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));

//Get an attachment in attachment variable class
Attachment att = new Attachment("D:\\test.txt");

//add attachment to email
mail.Attachments.Add(att);

//Put email subject
mail.Subject = "Send Email with Gmail using ASP.NET";


//Put email body
string Body = "Put your Email Body TEXT here";

// Here you can put HTML string if you want email to be sent in HTML format.
mail.Body = Body;
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "PASSWORD");

//keep ssl true if you have to send email with secured socket layer
smtp.EnableSsl = false;

smtp.Send(mail);


----------------------------------------------

code



now your code is ready to send email using ASP.NET

Exception points
There are so many factor that may affects your email sending code, here is list,

1. Firewall block port 587 (This is port for smtp gmail server)

2. Internet connection is not available on machine or internet connection has face some obstacles while pinging

3. Due to some typo mistake UserName and password may be get wrong

4. Enable SSL is block email sending

5. POP settings for smtp server in your Gmail account is disabled


Comments

Author: mausumi30 May 2012 Member Level: Gold   Points : 0

hi

Thanks 4 this article.
it is helpful .



from
mausumi

Guest Author: samad06 Jun 2012

not send.

1. Firewall block port 587 (This is port for smtp gmail server)???

4. Enable SSL is block email sending???



  • 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:
    Email: