Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Programmatically send emails using Google mail server and System.Net.Mail.SmtpClient

Did you know you can use your gmail mail server to send and receive emails programmatically for free?

It's true; gmail allows you to use their mail servers to send email from your applications. If you combine that with the ASP.NET System.Net.Mail.SmtpClient class, you are ready to use your free gmail account and server. 

Here is the Google mail server info you need:

Mail Host: smtp.gmail.com

Port: 587

You can use this info to send email from any Web or Windows application:

How to use in ASP.NET Web Application:

·        In your web.config file, Add the following configuration section:

 

<system.net>

   <mailSettings>

      <smtp from=your_gmail_email@gmail.com>

         <network host="smtp.gmail.com"

                  password="your_gmail_password"

                  port="587"

            userName="your_gmail_email@gmail.com"/>

      </smtp>

   </mailSettings>

</system.net>

 

·        Now from any where in your ASP.NET web application, you can send email using System.Net.Mail.SmtpClient as follows:

 

using System.Net.Mail;

...

SmtpClient mailClient = new SmtpClient();

mailClient.EnableSsl = true;

mailClient.Send(from, to, subject, body);

This is very useful in many ways, for example, when you want to get notified about certain events that happen on your site. In my other article: "Ever wanted to know when Google bot visits your site?" I utilize the code above to know when a certain visitor comes to my site, specifically in this case, Google's spider.

How to use in .NET Windows Application:

·        From your Windows application, you will need to setup the Network Credentials. Here is the code:

 

using System.Net.Mail;

using System.Net;

... 

SmtpClient mailClient = new SmtpClient(

    "smtp.gmail.com", "587");

mailClient.EnableSsl = true;

NetworkCredential cred = new NetworkCredential(

    "your_gmail_username",

    "your_gmail_password");

mailClient.Credentials = cred;

mailClient.Send("from_me@gmail.com", "to_you@gmail.com",

    "subject goes here", "email body goes here");

And that's it! Please use the above code responsively :)

kick it on DotNetKicks.com

Feedback

Posted on 7/17/2011 5:41:53 AM

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Posted on 6/12/2011 1:29:30 PM

Fantastic! It worked for me! A real good handy one!

Posted on 1/12/2011 8:04:29 AM

Well done! Thanks a million

Posted on 9/1/2010 1:46:14 AM

Super!!!

Thanks a million boss....u made my day...err....week!!!! Keep up ur good work....u are helping thousands of people!!

Posted on 8/19/2010 3:59:25 AM

its really work
VERY NICE
thanks a lot
keep coding

Posted on 8/5/2010 11:54:16 PM

I so enjoy your writing, thank you for posting an interesting article and good.

Posted on 5/16/2010 7:47:28 AM

Good Code

It works.............

Thank You for sharing

Posted on 1/23/2010 7:46:38 PM

Thanks saved my day...

Posted on 11/4/2009 2:26:40 AM

when i m sending the mail
its showing on the browser "waiting for localhost"
what is the problem? i m not getting.
could u please help me.

Posted on 7/9/2009 8:11:38 AM

Pls Anyone can help?
I get this Error
"Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

when i send mail.

Posted on 4/28/2009 1:00:39 PM

great code....
Nice one.....
thanks for sharing.

Posted on 2/6/2009 2:23:42 AM

Good code. it really works

Posted on 2/5/2009 5:54:30 AM

Hi, this code was working for me correctly, but since last 2 weeks it has stopped working. In my error log, it shows that connection was actively refused by this server ip address. does anybody have any idea why this is happening?

Posted on 1/29/2009 7:23:22 AM

OMG!!!!

I was so worried that integrating an email feature in my webpage will be so difficult.

You just gave me a solution in less than 10 lines!

GREAT WORK!!!

Posted on 1/12/2009 8:31:31 PM

Tryed this code and received the error:

No connection could be made because the target machine actively refused it.

How can this be resolved?

Posted on 10/17/2008 6:30:49 AM

Hi there,

I came across this website searching for answer for email verifier software. i came across atomic email verifier btu the problem is it needs access to port 25 whihc is blocked by my isp, however i can still use outlook for my domain email s using port 587. This software says you use proxy ip like socks 4/5/4A......and dns server....i am really confused as what could be the solution to this...awaiting your replies...thanks in advance for your time .....pls do suggest if you have some other software in mind for verifying my email list.....will appreacite your time and effort....

Posted on 9/10/2008 2:15:58 AM

How we can receive mail programetically

Posted on 8/10/2008 9:14:56 AM

u r great yaaar.....
it is very helpful for my project
thanks..

Posted on 8/6/2008 3:59:03 PM

Hi,
links that you provided for reading mail is not working.can u plzz provide any useful material asap. itz very urgent.

Posted on 7/3/2008 8:37:34 PM

nice code...
thanks
what if i have an attactment in my mail?
how to send it? plz help..

Posted on 6/18/2008 4:39:04 PM

Nice Work Man , ;) i have an answer for Shuaib , use instead of smpt.gmail.com , use smtp.googlemail.com
//yahoo ---> smtp.mail.yahoo.com (port 25)
//gmail ---> smtp.gmail.com (SSL enabled, port 465)
//AOL ---> smtp.aol.com
//Netscape ---> smtp.isp.netscape.com (port 25)

Posted on 6/12/2008 5:21:34 PM

Hi,
How about if you are using the Google Apps email for your site? the SMTP is not smtp.gmail.com, or is it? I used the smtp.gmail.com but it did not work. Any hlep would be appreciate!

Thanks

Posted on 3/24/2008 8:55:58 AM

It's really nice one but can anyone tell me how to receive gmail mails in the database through .net application

Posted on 1/29/2008 8:13:52 AM

Dear Friends,

On my web server 587 is not open can you help me how i can use my google account to send email from mywebsite?

Please let me know

Thanks in advance..

Nital Patel

Posted on 1/28/2008 1:35:57 PM

Very Well Done...Keep it up man...Hats off to you for sharing...

Posted on 1/12/2008 6:47:01 AM

Good one...thanks for sharing.

Posted on 12/11/2007 2:27:16 AM

it was really helpfull. thank u so much

Posted on 9/23/2007 11:12:12 PM

Thats a pretty neat trick! Thanks for sharing...
PK ;-)

Posted on 9/3/2007 11:35:45 PM

DAMN!.............
its really work................
VERY NICE..............
thanks a lot man............
keep coding..............

Posted on 9/3/2007 11:33:45 PM

DAMN!.............
its really work................
VERY NICE..............
thanks a lot man............
keep coding..............

Posted on 9/3/2007 11:32:23 PM

DAMN!.............
its really work................
VERY NICE..............
thanks a lot man............
keep coding..............

Posted on 8/17/2007 8:14:09 AM

this code is not working.
when i run this code an exception is occured "send failure"

Posted on 8/9/2007 10:59:55 PM

My operating system is windows vista , in this i configure windows mail for google mail. I want to know pop settings , servers name , there port number.and its settings

Posted on 7/5/2007 6:57:03 AM

Very nice article.. keep it up

Posted on 4/11/2007 1:55:07 PM

How do you recieve emails from gmail programatically ?

Posted on 4/11/2007 5:25:33 AM

Reading POP3 emails is a bit different, Here are a couple of helpful links:

http://www.codeproject.com/useritems/Pop3MailClient.asp
http://www.codeproject.com/cs/internet/popapp.asp

Posted on 4/11/2007 3:58:29 AM

Love to see how you read the mails from GMail?

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author