Click here to Skip to main content
12,701,314 members (36,556 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

Send Emails in ASP.NET using Gmail Credentials

, 10 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article contains code to send email using Gmail accounts.


As ASP.NET web developers, we often have to include an email sending module in our websites. When I was completing my third year project in the university, I had the same requirement in my web project. I had to go through so many web sites and blogs to find a solution. Using the class file included with this article, you can send emails by entering your Gmail account credentials.


Make sure to instantiate the instance of this class with the correct parameters.

Using the Code

To use this class, add this class to the App_Code folder in your website. Then, create a web page to get the required user input to send a mail. E.g.: To, Subject, and Message. Then, edit the username and password files as you want. Here, to allow a user to send mails, make sure you get the username and password using the website. Then, add two more variables to pass the username and password to the application.

Edit sendMail() as follows:

public static void sendMail(string to, string from, string subject, 
              string body,string username, string password)
    ///Smtp config
    SmtpClient client = new SmtpClient("", 465);
    // Edit password and username
    client.Credentials = new NetworkCredential(""+username+"", password);
    client.EnableSsl = true;

    Mail.Fields[""] = 
    Mail.Fields[""] = password;

If you do not allow every user to send email, but only for certain users, then you can use a common email for all the email senders, succh as Then, you don't need to change any other method definition, but make sure to change the username and the password.

client.Credentials = new NetworkCredential("", "PASSWORD");
Mail.Fields[""] = 
Mail.Fields[""] = "PASSWORD";

Points of Interest

The main feature in this class is it has a back up plan, just in case, to invoke. The mail sending method is sendMail(), but if it fails, it invokes an alternate mail sending method SendMailAlt() using the appropriate attributes.

But, this second method does not allow us to send attachments. So don't allow the user to add attachments to any of the methods.


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


About the Author

Harshana Eranga Martin
Engineer Computer Science & Engineering,Uni. Of Moratuwa.
Sri Lanka Sri Lanka
I'm currently an undergraduate in the University of
Moratuwa in the faculty of Engineering and my field of specialization is Computer Science & Engineering.

My Hometown is Matugama which is situated in the Kalutara district & i attended C.W.W. Kannangara central College,Matgama & St.Mary's Colege, Matugama.

I entered to the university in the Mid 2006 & will pass out in the Mid 2010.

You may also be interested in...


Comments and Discussions

AnswerRe: ms outlook installation issue ? Pin
Harshana Eranga Martin1-Apr-10 7:39
memberHarshana Eranga Martin1-Apr-10 7:39 
Generalits great Pin
mahendra_apte8510-Apr-09 16:08
membermahendra_apte8510-Apr-09 16:08 
Questioncan not resolve server Pin
thilina13-Mar-09 22:29
memberthilina13-Mar-09 22:29 
Generalerror Pin
Member 47649556-Mar-09 6:23
memberMember 47649556-Mar-09 6:23 
AnswerRe: error Pin
Harshana Eranga Martin7-Mar-09 1:48
memberHarshana Eranga Martin7-Mar-09 1:48 
GeneralGood article Pin
Donsw25-Jan-09 17:33
memberDonsw25-Jan-09 17:33 
GeneralThe transport failed to connect to the server Pin
ravifree17-Nov-08 5:16
memberravifree17-Nov-08 5:16 
AnswerRe: The transport failed to connect to the servers Pin
Harshana Eranga Martin30-Nov-08 20:50
memberHarshana Eranga Martin30-Nov-08 20:50 
GeneralGreat Work!!! and a question Pin
aakash13singh18-Oct-08 12:31
memberaakash13singh18-Oct-08 12:31 
AnswerRe: Great Work!!! and a question Pin
Kanedogg0828-Oct-08 15:04
memberKanedogg0828-Oct-08 15:04 
GeneralRe: Great Work!!! and a question Pin
Harshana Eranga Martin30-Nov-08 20:59
memberHarshana Eranga Martin30-Nov-08 20:59 
GeneralFailure sending Mail... Pin
quitchat17-Oct-08 20:26
memberquitchat17-Oct-08 20:26 
GeneralRe: Failure sending Mail... Pin
Harshana Eranga Martin18-Oct-08 3:42
memberHarshana Eranga Martin18-Oct-08 3:42 
GeneralRe: Failure sending Mail... Pin
quitchat18-Oct-08 5:33
memberquitchat18-Oct-08 5:33 
AnswerRe: Failure sending Mail... Pin
Harshana Eranga Martin18-Oct-08 9:25
memberHarshana Eranga Martin18-Oct-08 9:25 
QuestionGmail? Pin
jinksk15-Oct-08 6:30
memberjinksk15-Oct-08 6:30 
AnswerRe: Gmail? Pin
Harshana Eranga Martin18-Oct-08 3:53
memberHarshana Eranga Martin18-Oct-08 3:53 
GeneralRe: Gmail? Pin
GriffinPeter10-Nov-08 11:41
memberGriffinPeter10-Nov-08 11:41 
AnswerRe: Gmail? Pin
Harshana Eranga Martin10-Nov-08 18:30
memberHarshana Eranga Martin10-Nov-08 18:30 
GeneralWinforms Version Video Pin
Pat Tormey14-Oct-08 1:52
memberPat Tormey14-Oct-08 1:52 
GeneralThe article updated Pin
Harshana Eranga Martin7-Oct-08 9:20
memberHarshana Eranga Martin7-Oct-08 9:20 
Questionsending emails asynchronously? Pin
yassir hannoun7-Oct-08 7:37
memberyassir hannoun7-Oct-08 7:37 
AnswerRe: sending emails asynchronously? Pin
Harshana Eranga Martin7-Oct-08 7:44
memberHarshana Eranga Martin7-Oct-08 7:44 
GeneralsendMail method seam alway to be fail, and cause SendMailAlt method. Pin
leegool7-Oct-08 6:45
memberleegool7-Oct-08 6:45 
GeneralRe: sendMail method seam alway to be fail, and cause SendMailAlt method. Pin
Harshana Eranga Martin7-Oct-08 7:36
memberHarshana Eranga Martin7-Oct-08 7:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 10 Nov 2008
Article Copyright 2008 by Harshana Eranga Martin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid