![]() |
Web Development »
ASP.NET »
Howto
Intermediate
License: The Code Project Open License (CPOL)
Send Emails in ASP.NET using Gmail CredentialsBy Harshana Eranga MartinThis article contains code to send email using Gmail accounts. |
HTML, C#2.0, C#3.0.NET2.0, .NET3.0, .NET3.5, ASP, ASP.NET, IIS5.1, IIS6, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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";
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.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Nov 2008 Editor: Sean Ewington |
Copyright 2008 by Harshana Eranga Martin Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |