Mariya Atanasova's Blog

Sending e-mail using SmtpClient and Gmail

The sample below used SmtpClient to send e-mail from your gmail account using your gmail username and password.

 

 

using System;

using System.Net;

using System.Net.Mail;

 

namespace GMailSample

{

    class SimpleSmtpSend

    {

        static void Main(string[] args)

        {

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);           

            client.EnableSsl = true;

            MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");           

            MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");

            MailMessage message = new MailMessage(from, to);

            message.Body = "This is a test e-mail message sent using gmail as a relay server ";

            message.Subject = "Gmail test email with SSL and Credentials";

            NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");           

            client.Credentials = myCreds;

            try

            {

                client.Send(message);

            }

            catch (Exception ex)

            {

                Console.WriteLine("Exception is:" + ex.ToString());

            }

            Console.WriteLine("Goodbye.");

        }

    }

}

 

What happens here is that SmtpClient sends your e-mail to a relay server (in this case the gmail web mail server) and then this relay server sends it to its specified destination. Of course you're not limited to just gmail. The relay server can be another mail server, or a SMTP server/service - you can use a third party one or set your own. I'll update the post to include more detail on this later.

 

You can easily expand this sample to a more elaborate one - with attachments, html content, embeded images, etc. For more detail please refer to our documentation on msdn2 here:

http://msdn2.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
There is also a very good site containig basic and more advanced samples here:
http://www.systemnetmail.com/faq/3.aspx
http://www.systemnetmail.com/faq/4.aspx

Published Thursday, June 15, 2006 11:23 PM by mariya

Comments

 

From My Desktop To The Internet... said:

I thought this was a pretty cool post: http://blogs.msdn.com/mariya/archive/2006/06/15/633007.aspx
Not...
June 15, 2006 7:50 PM
 

Sanket Bakshi said:

Interesting one.
But isnt it more like advertising about google?
How about something like using LIVE programatically?
June 17, 2006 5:08 AM
 

coryc13 said:

First, thank you as this has been a very helpful starting point as I am just getting into .Net and C#. However, I have discovered a problem that seems to have been avoided in most discussions. You should be able to use port 465, but using your example (and others) fails to work on this port. I have tried this on my own server as well. I cannot authenticate on port 465. I did come across a post (forgive me for not rememberering where I saw it) that mentioned this is a problem with .Net and it's inability to authenticate using the Exchange structure - I'm paraphrasing.

So, I would be interested if anyone has seen this same problem or if they have actually been able to use port 465 on any server (gmail or others) and if there is a patch for .Net to make this work correctly.

November 28, 2006 2:25 PM
 

Korayem said:

I too have the same issue.

@coryc13:

I think you are referring to this http://www.systemnetmail.com/faq/5.2.aspx

If anyone has found a solution to this, can you please share it with me?

March 22, 2007 10:12 AM
 

Danik Kovalevsky said:

Thank you! Very useful code :-)

June 8, 2007 11:15 AM
 

System.Net.Mail para cualquier correo | hilpers said:

January 20, 2009 2:52 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker