Click here to Skip to main content
Click here to Skip to main content

Send Emails in ASP.NET using Gmail Credentials

By , 10 Nov 2008
 

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberSandeep Ramani9 Sep '11 - 2:44 
Questionms outlook installation issue ?membersuis31 Mar '10 - 4:52 
AnswerRe: ms outlook installation issue ?memberHarshana Eranga Martin31 Mar '10 - 5:08 
GeneralRe: ms outlook installation issue ?membersuis31 Mar '10 - 5:50 
AnswerRe: ms outlook installation issue ?memberHarshana Eranga Martin31 Mar '10 - 7:06 
GeneralRe: ms outlook installation issue ?membersuis31 Mar '10 - 22:36 
GeneralRe: ms outlook installation issue ?memberHarshana Eranga Martin31 Mar '10 - 22:57 
GeneralRe: ms outlook installation issue ?membersuis31 Mar '10 - 23:09 
AnswerRe: ms outlook installation issue ? [modified]memberHarshana Eranga Martin31 Mar '10 - 23:25 
GeneralRe: ms outlook installation issue ?membersuis1 Apr '10 - 5:46 
AnswerRe: ms outlook installation issue ?memberHarshana Eranga Martin1 Apr '10 - 6:39 
Generalits greatmembermahendra_apte8510 Apr '09 - 15:08 
Questioncan not resolve servermemberthilina13 Mar '09 - 21:29 
GeneralerrormemberMember 47649556 Mar '09 - 5:23 
AnswerRe: errormemberHarshana Eranga Martin7 Mar '09 - 0:48 
GeneralGood articlememberDonsw25 Jan '09 - 16:33 
GeneralThe transport failed to connect to the servermemberravifree17 Nov '08 - 4:16 
AnswerRe: The transport failed to connect to the serversmemberHarshana Eranga Martin30 Nov '08 - 19:50 
GeneralGreat Work!!! and a questionmemberaakash13singh18 Oct '08 - 11:31 
AnswerRe: Great Work!!! and a questionmemberKanedogg0828 Oct '08 - 14:04 
GeneralRe: Great Work!!! and a questionmemberHarshana Eranga Martin30 Nov '08 - 19:59 
GeneralFailure sending Mail...memberquitchat17 Oct '08 - 19:26 
GeneralRe: Failure sending Mail...memberHarshana Eranga Martin18 Oct '08 - 2:42 
GeneralRe: Failure sending Mail...memberquitchat18 Oct '08 - 4:33 
AnswerRe: Failure sending Mail...memberHarshana Eranga Martin18 Oct '08 - 8:25 

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

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