Click here to Skip to main content
15,881,424 members
Articles / Web Development / HTML
Article

Send Emails in ASP.NET using Gmail Credentials

Rate me:
Please Sign up or sign in to vote.
3.54/5 (26 votes)
10 Nov 2008CPOL1 min read 139.4K   2.5K   77   40
This article contains code to send email using Gmail accounts.

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:

C#
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.

C#
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)


Written By
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.

Comments and Discussions

 
QuestionGmail? Pin
jinksk15-Oct-08 5:30
jinksk15-Oct-08 5:30 
AnswerRe: Gmail? Pin
Harshana Eranga Martin18-Oct-08 2:53
Harshana Eranga Martin18-Oct-08 2:53 
GeneralRe: Gmail? Pin
CARPETBURNER10-Nov-08 10:41
CARPETBURNER10-Nov-08 10:41 
AnswerRe: Gmail? Pin
Harshana Eranga Martin10-Nov-08 17:30
Harshana Eranga Martin10-Nov-08 17:30 
GeneralWinforms Version Video Pin
Pat Tormey14-Oct-08 0:52
Pat Tormey14-Oct-08 0:52 
GeneralThe article updated Pin
Harshana Eranga Martin7-Oct-08 8:20
Harshana Eranga Martin7-Oct-08 8:20 
Questionsending emails asynchronously? Pin
yassir hannoun7-Oct-08 6:37
yassir hannoun7-Oct-08 6:37 
AnswerRe: sending emails asynchronously? Pin
Harshana Eranga Martin7-Oct-08 6:44
Harshana Eranga Martin7-Oct-08 6:44 
Yeah. of course. That method is there to send mail asynchronously.

Department of Computer Science & Engineering,
University Of Moratuwa,
Sri Lanka.

GeneralsendMail method seam alway to be fail, and cause SendMailAlt method. Pin
leegool7-Oct-08 5:45
leegool7-Oct-08 5:45 
GeneralRe: sendMail method seam alway to be fail, and cause SendMailAlt method. Pin
Harshana Eranga Martin7-Oct-08 6:36
Harshana Eranga Martin7-Oct-08 6:36 
GeneralPossible typo Pin
redwolf68797-Oct-08 5:17
redwolf68797-Oct-08 5:17 
GeneralRe: Possible typo Pin
Harshana Eranga Martin7-Oct-08 6:29
Harshana Eranga Martin7-Oct-08 6:29 
GeneralRe: Possible typo Pin
redwolf68797-Oct-08 6:34
redwolf68797-Oct-08 6:34 
GeneralMINI Project about Online Shopping Using ASP .NET with VB Pin
srvishnukumar6-Oct-08 21:25
srvishnukumar6-Oct-08 21:25 
GeneralRe: MINI Project about Online Shopping Using ASP .NET with VB Pin
ManiVannan M14-Oct-08 0:26
ManiVannan M14-Oct-08 0:26 

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.