Click here to Skip to main content
6,629,377 members and growing! (21,648 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Send Emails in ASP.NET using Gmail Credentials

By Harshana Eranga Martin

This article contains code to send email using Gmail accounts.
HTML, C# 2.0, C# 3.0.NET 2.0, .NET 3.0, .NET 3.5, ASP, ASP.NET, IIS 5.1, IIS 6, Dev
Posted:6 Oct 2008
Updated:10 Nov 2008
Views:23,717
Bookmarked:63 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.35 Rating: 3.34 out of 5
3 votes, 15.0%
1
1 vote, 5.0%
2
3 votes, 15.0%
3
5 votes, 25.0%
4
8 votes, 40.0%
5

Introduction

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.

Background

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("smtp.gmail.com", 465);
    // Edit password and username
    client.Credentials = new NetworkCredential(""+username+"@gmail.com", password);
    client.EnableSsl = true;

    Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = 
                ""+username+"@gmail.com";
    Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = 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 admin@obrs.com. 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("USERNAME@gmil.com", "PASSWORD");
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = 
            "USERNAME@gmail.com";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "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.

License

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


Member
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.
Occupation: Engineer
Company: Computer Science & Engineering,Uni. Of Moratuwa.
Location: Sri Lanka Sri Lanka

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generalits great Pinmembermahendra_apte8516:08 10 Apr '09  
Generalcan not resolve server Pinmemberthilina22:29 13 Mar '09  
Generalerror PinmemberMember 47649556:23 6 Mar '09  
AnswerRe: error PinmemberHarshana Eranga Martin1:48 7 Mar '09  
GeneralGood article PinmemberDonsw17:33 25 Jan '09  
GeneralThe transport failed to connect to the server Pinmemberravifree5:16 17 Nov '08  
AnswerRe: The transport failed to connect to the servers PinmemberHarshana Eranga Martin20:50 30 Nov '08  
GeneralGreat Work!!! and a question Pinmemberaakash13singh12:31 18 Oct '08  
AnswerRe: Great Work!!! and a question PinmemberKanedogg0815:04 28 Oct '08  
GeneralRe: Great Work!!! and a question PinmemberHarshana Eranga Martin20:59 30 Nov '08  
GeneralFailure sending Mail... Pinmemberquitchat20:26 17 Oct '08  
GeneralRe: Failure sending Mail... PinmemberHarshana Eranga Martin3:42 18 Oct '08  
GeneralRe: Failure sending Mail... Pinmemberquitchat5:33 18 Oct '08  
AnswerRe: Failure sending Mail... PinmemberHarshana Eranga Martin9:25 18 Oct '08  
RantGmail? Pinmemberjinksk6:30 15 Oct '08  
AnswerRe: Gmail? PinmemberHarshana Eranga Martin3:53 18 Oct '08  
GeneralRe: Gmail? PinmemberGriffinPeter11:41 10 Nov '08  
AnswerRe: Gmail? PinmemberHarshana Eranga Martin18:30 10 Nov '08  
GeneralWinforms Version Video PinmemberPat Tormey1:52 14 Oct '08  
GeneralThe article updated PinmemberHarshana Eranga Martin9:20 7 Oct '08  
Generalsending emails asynchronously? Pinmemberyassir hannoun7:37 7 Oct '08  
GeneralRe: sending emails asynchronously? PinmemberHarshana Eranga Martin7:44 7 Oct '08  
GeneralsendMail method seam alway to be fail, and cause SendMailAlt method. Pinmemberleegool6:45 7 Oct '08  
GeneralRe: sendMail method seam alway to be fail, and cause SendMailAlt method. PinmemberHarshana Eranga Martin7:36 7 Oct '08  
GeneralPossible typo Pinmemberredwolf68796:17 7 Oct '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 10 Nov 2008
Editor: Sean Ewington
Copyright 2008 by Harshana Eranga Martin
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project