5,443,978 members and growing! (20,709 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Beginner License: The Code Project Open License (CPOL)

Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)

By Sumit Kumar Pranav

This article demonstrate the way to send Email suing Gmail account in ASP.net
.NET (.NET 2.0, .NET), ASP.NET, Visual Studio (Visual Studio, VS2005)

Posted: 15 Jun 2008
Updated: 15 Jun 2008
Views: 2,545
Bookmarked: 3 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 1.94 Rating: 2.29 out of 5
3 votes, 42.9%
1
1 vote, 14.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
3 votes, 42.9%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Sending Email is a basic task of a web application for many purposes like- Error logging, Reporting, Newsletters, and Performance alerting as well as for many other reasons.

If you have taken 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. Some times you don't have SMTP server accounts and you may want to send email. Or Sometimes, for other reasons also you may want to use third-party SMTP sever for sending E-mails .

Gmail is a good solution for this purpose. Gmail provides SMTP and POP access which can be utilized for sending and receiving E-mails.
Here in this tutorial we will be using a Gmail account for sending email by SMTP.

=========================================

Note

1. Gmail has a fixed number of quota for sending emails per day, means you may not be able to send unlimited number of e-mails using Gmail due to anti-spamming restrictions.

2. You may not be able to send same e-mail to unlimited number of people, because Gmail's anti-spamming engine restricts for this too.

So, Gmail can be used for sending limited number of e-mails daily to select people only. If you still need to send numerous e-mails to numerous people you can register for a Google Group account and can use that e-mail address to send email to registered members of that group. I will come up with that tutorial in near future.

Using the code

Requirements:
1. An active Gmail account is required to test this application.
2. Visual Studio 2005

Here we go:

Step 1:
Create a new web application in visual studio and add a Web form to it.
Next add a Button to the form and double click this button to bring the code for click event of this button in the code window. Copy paste the following code to the click event.


Step 2: Run the web application and click on the Button to send the mail. Check the E-mail whether you get the mail successfully or not.


Note: Don't forget to replace the To and From fields as well as the correct password for successfully sending the mail.

            
string from = me@gmail.com; //Replace this with your own correct Gmail Address

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

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");

client.Port = 587; // Gmail works on this port
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 );
        } // end try 
          


Points of Interest

Some-times it takes 2-3 minutes or more to reach the mail to the destination server. So check after a while, if you do not get it soon.

Please visit my blog for more interesting articles and tutorials.
Find this same tutorial at my blog here.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sumit Kumar Pranav


I am a programer by profession, innovator by nature and an artist from the inner essence. I love inventing new things and reading technical and scientific articles.

I have been programing from my childhood days. I started my programing life in my high school days, in class 6th when I was used to write a lot programs in BASIC language.

Then, I learnt Visual Basic-6 language and written hundreds of programs, varying from Graphics (including Direct-x, Open GL), Win 32 API, Database Application, Socket programing and Signaling.

Graphics was my then favorite field of interest. I wrote few award wining graphics applications, one called Magic Brush which won several national and international prizes and praise. Later I got fascinated towards web technologies that encouraged me to write an anti-spam software recognition engine called "Spamocide".

It was the journey of a hobbyist programer. Then, started my academic programing journey which continues. I love to program for research projects specially in the field of computer security, Internet, Ontology, Artificial Intelligence and many more.

In 2007 my software research project called ECIGS was among top 5 softwares in Microsoft Imagine Cup national finals.

http://www.microsoft.com/india/student/ic07/T_ECIGS.aspx

Now a days I do freelance programing and provide my consultation to companies in the field of web security, web application development, performance testing etc...

One can reach to me at this e-mail address: spscreen-pub@yahoo.co.in

My Programing Blog: http://codepalace.blogspot.com/



Occupation: Software Developer
Company: Techmint Solutions
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralException while using your codememberrohan_daxini3:04 24 Aug '08  
GeneralNice ArticlememberDeepak.M19:59 17 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Jun 2008
Editor:
Copyright 2008 by Sumit Kumar Pranav
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project