Click here to Skip to main content
5,786,882 members and growing! (21,575 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# 3.0, C# 2.0, C#Windows, .NET, .NET 3.0, .NET 3.5, .NET 2.0, NT4, ASP, ASP.NET, IIS 5, IIS 5.1, IIS 6, IIS, Dev

Posted: 6 Oct 2008
Updated: 10 Nov 2008
Views: 9,709
Bookmarked: 52 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 4.16 Rating: 3.31 out of 5
3 votes, 16.7%
1
1 vote, 5.6%
2
2 votes, 11.1%
3
5 votes, 27.8%
4
7 votes, 38.9%
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



Occupation: Other
Company: Department of computer science & Engineering, Uni
Location: Sri Lanka Sri Lanka

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralThe transport failed to connect to the servermemberravifree5:16 17 Nov '08  
AnswerRe: The transport failed to connect to the serversmemberHarshana Eranga Martin20:50 30 Nov '08  
GeneralGreat Work!!! and a questionmemberaakash13singh12:31 18 Oct '08  
AnswerRe: Great Work!!! and a questionmemberKanedogg0815:04 28 Oct '08  
GeneralRe: Great Work!!! and a questionmemberHarshana Eranga Martin20:59 30 Nov '08  
GeneralFailure sending Mail...memberquitchat20:26 17 Oct '08  
GeneralRe: Failure sending Mail...memberHarshana Eranga Martin3:42 18 Oct '08  
GeneralRe: Failure sending Mail...memberquitchat5:33 18 Oct '08  
AnswerRe: Failure sending Mail...memberHarshana Eranga Martin9:25 18 Oct '08  
RantGmail?memberjinksk6:30 15 Oct '08  
AnswerRe: Gmail?memberHarshana Eranga Martin3:53 18 Oct '08  
GeneralRe: Gmail?memberGriffinPeter11:41 10 Nov '08  
AnswerRe: Gmail?memberHarshana Eranga Martin18:30 10 Nov '08  
GeneralWinforms Version VideomemberPat Tormey1:52 14 Oct '08  
GeneralThe article updatedmemberHarshana Eranga Martin9:20 7 Oct '08  
Generalsending emails asynchronously?memberyassir hannoun7:37 7 Oct '08  
GeneralRe: sending emails asynchronously?memberHarshana Eranga Martin7:44 7 Oct '08  
GeneralsendMail method seam alway to be fail, and cause SendMailAlt method.memberleegool6:45 7 Oct '08  
GeneralRe: sendMail method seam alway to be fail, and cause SendMailAlt method.memberHarshana Eranga Martin7:36 7 Oct '08  
GeneralPossible typomemberredwolf68796:17 7 Oct '08  
GeneralRe: Possible typomemberHarshana Eranga Martin7:29 7 Oct '08  
GeneralRe: Possible typomemberredwolf68797:34 7 Oct '08  
GeneralMINI Project about Online Shopping Using ASP .NET with VBmembersrvishnukumar22:25 6 Oct '08  
GeneralRe: MINI Project about Online Shopping Using ASP .NET with VBmemberdotnethunt1:26 14 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
Web10 | Advertise on the Code Project