Click here to Skip to main content
13,196,286 members (47,134 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 15 Jun 2008

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

, 15 Jun 2008
Rate this:
Please Sign up or sign in to vote.
This article demonstrate the way to send Email suing Gmail account in


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.



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

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 =; //Replace this with your own correct Gmail Address

string to = //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();<o:p />
<o:p> </o:p>mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);<o:p />
mail.Subject = "This is a test mail" ;<o:p />
mail.SubjectEncoding = System.Text.Encoding.UTF8;<o:p />
mail.Body = "This is Email Body Text";<o:p />
mail.BodyEncoding = System.Text.Encoding.UTF8;<o:p />
mail.IsBodyHtml = true ;<o:p />
mail.Priority = MailPriority.High;<o:p />
<o:p />
SmtpClient client = new SmtpClient();<o:p />
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");
<o:p />
client.Port = 587; // Gmail works on this port<o:p />
<o:p />client.Host = "";<o:p />
client.EnableSsl = true; //Gmail works on Server Secured Layer
       try<o:p />
        {<o:p />
            client.Send(mail);<o:p />
        }<o:p />
        catch (Exception ex)<o:p /> 
        {<o:p />
            Exception ex2 = ex;<o:p />
            string errorMessage = string.Empty;<o:p /> 
            while (ex2 != null)<o:p />
            {<o:p />
                errorMessage += ex2.ToString();<o:p />
                ex2 = ex2.InnerException;<o:p />
            }<o:p />
<o:p> </o:p>  HttpContext.Current.Response.Write(errorMessage );<o:p />
        } // end try 
<pre />

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.


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


About the Author

Sumit P
India India
I am a software architect by profession, innovator by nature and an artist from the inner essence.

I have been programing from my childhood days. I started my programing life in my high school days, in 6th grade 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.

One can reach to me at this e-mail address:

My Programing Blog:

You may also be interested in...


Comments and Discussions

QuestionMessage Closed Pin
27-Mar-14 2:23
memberAyesha Sharma27-Mar-14 2:23 
QuestionMessage Closed Pin
18-Feb-14 23:02
memberMember 1014497618-Feb-14 23:02 
QuestionMessage Closed Pin
18-Feb-14 22:52
memberMember 1014497618-Feb-14 22:52 
QuestionSSL must not be enabled for pickup-directory delivery methods. Pin
Member 997990824-Sep-13 1:31
memberMember 997990824-Sep-13 1:31 
GeneralMy vote of 1 Pin
Member 1000585326-Jun-13 17:59
memberMember 1000585326-Jun-13 17:59 
GeneralMy vote of 1 Pin
Amol Rawke Pune15-Oct-12 2:02
memberAmol Rawke Pune15-Oct-12 2:02 
AnswerSending Email Pin
Mousumi270825-May-12 3:04
memberMousumi270825-May-12 3:04 
GeneralMy vote of 3 Pin
kaisernayan20-Dec-11 20:32
memberkaisernayan20-Dec-11 20:32 
GeneralA potentially hazardous server error Pin
shikhagupta2129-Apr-11 6:24
membershikhagupta2129-Apr-11 6:24 
GeneralRe: A potentially hazardous server error Pin
Sumit Kumar Pranav19-Apr-12 19:34
memberSumit Kumar Pranav19-Apr-12 19:34 
GeneralRe: A potentially hazardous server error Pin
Deepesh Lad10-Feb-13 0:29
memberDeepesh Lad10-Feb-13 0:29 
GeneralNice Article Pin
shikhar2102896-Apr-11 3:47
membershikhar2102896-Apr-11 3:47 
Generalthanks Pin
tmp12027-Feb-11 19:50
membertmp12027-Feb-11 19:50 
QuestionWill this work using the development server, - or do you have to use IIS ? Pin
mnemonic692-Jan-11 0:23
membermnemonic692-Jan-11 0:23 
AnswerRe: Will this work using the development server, - or do you have to use IIS ? Pin
loyal ginger2-Jan-11 10:50
memberloyal ginger2-Jan-11 10:50 
AnswerRe: Will this work using the development server, - or do you have to use IIS ? Pin
Sumit Kumar Pranav19-Apr-12 19:35
memberSumit Kumar Pranav19-Apr-12 19:35 
GeneralNice Artical Pin
S Gnana Prakash7-Jul-09 3:05
memberS Gnana Prakash7-Jul-09 3:05 
Generalneed it in c++ Pin
Mohammad Minhazul Alam20-May-09 22:10
memberMohammad Minhazul Alam20-May-09 22:10 
GeneralNeeds to be more generic. Pin
fly90425-Apr-09 13:18
memberfly90425-Apr-09 13:18 
GeneralExcelent Work Pin
Mohamed Ibrahim Omar5-Apr-09 22:00
memberMohamed Ibrahim Omar5-Apr-09 22:00 
GeneralSolution Found Pin
Nauman6425-Mar-09 1:58
memberNauman6425-Mar-09 1:58 
GeneralException occured while using this code Pin
imrangmworld5-Oct-08 21:05
memberimrangmworld5-Oct-08 21:05 
GeneralRe: Exception occured while using this code Pin
Sumit Kumar Pranav19-Apr-12 19:37
memberSumit Kumar Pranav19-Apr-12 19:37 
GeneralException while using your code Pin
rohan_daxini24-Aug-08 2:04
memberrohan_daxini24-Aug-08 2:04 
GeneralException while using your code Pin
prateekfgiet15-Dec-08 20:00
memberprateekfgiet15-Dec-08 20:00 
GeneralNice Article Pin
Deepak.M17-Aug-08 18:59
memberDeepak.M17-Aug-08 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171019.1 | Last Updated 15 Jun 2008
Article Copyright 2008 by Sumit P
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid