Wednesday, August 18, 2010

How to send an email via GMail using .NET

It’s really easy to use GMail as an SMTP client:

public class GoogleSmtpTest
    public void SendEmailViaGmail()
        var message = new MailMessage(
            "Hi via Google SMTP",
            "some body");

        var client = new SmtpClient("")
            EnableSsl = true,
            Port = 587,
            Credentials = new NetworkCredential("", "xxx's gmail password")


The only thing to note is that the SSL port given in the documentation (465) doesn’t seem to work, but the TLS one (587) works fine.


James Skimming said...

Do you know if it also works with Google Apps accounts? If you don't I plan to give it a go soon, so I'll get back either way.

Mike Hadlow said...

Yup, works fine with Google Apps for Domains.

Richard OD said...

I like that- nice. Another thing many people don't realise is you can get the .NET mail API to write the mail out to a file, which is damn handy when testing: