Sending email via GMail in C#/.NET using SmtpClient

Updated on Friday, September 13, 2019

Gmail Logo

Here’s a quick code snippet that configures SmtpClient to send email using Gmail’s SMTP server:

This can be frustratingly difficult to get right so I’ll run through each setting quickly.

DeliveryMethod: Set to SmtpDeliveryMethod.Network, the alternatives are copying the email to a directory for pickup by a different application. We want to connect directly to Gmail.

UseDefaultCredentials: False, as we’ll be providing our own credentials later. Setting properties in the right order is apparently important for SmtpClient, although the Microsoft documentation doesn’t mention this. Make sure you set UseDefaultCredentials to false before setting the Credentials property.

EnableSsl: True. SSL or TLS is required.

Host: smtp.gmail.com for this example. Note that this server restricts you to sending 2,000 emails per day. There is a less restrictive option for G Suite customers (smtp-relay.gmail.com) and a more restrictive server that can only send messages to Gmail or G Suite addresses (aspmx.l.google.com).

Port: Google says to use 465 for SSL and 587 for TLS. I’ve found that 587 with EnableSSL set to true works fine.

Credentials: Your Gmail address and password in a NetworkCredential. If your account has 2 step (multi factor) authentication then this won’t work. You can generate an app password easily and use this instead of your regular password. It’s also possible to use OAuth.

In the comments below Shika Helmy suggests also setting the Timeout property to 20000.

You can now use smtp.Send() to send the email.

Because it’s likely to be your personal account and there is a 2,000 message cap on sending I’d only recommend using this for small scale projects. You don’t want to get your Gmail account blocked. For higher volume I’d look at using SendGrid or similar.

One last note - while I’ve used SmtpClient for all sorts of trivial email needs over the years the latest Microsoft documentation marks it obsolete and warns:

SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead

Hope this expanded article helps. I still come back here periodically to cut and paste rather than mess up the property order again...

Add your comment...

Related Posts

(All Code Posts)

(Published to the Fediverse as: Sending email via GMail in C#/.NET using SmtpClient #code #smtp #email #google How to use the C# SmtpClient to send an email through the Gmail SMTP server (updated for two factor authentication). )

Android: Insane Contacts Storage

Updated on Sunday, November 6, 2022

Oh no:

Low on space (Android)

My phone keeps running out of space. A little sleuthing under Manage Applications shows that Contacts Storage is using over 32MB. Can’t move it to the SD Card – I guess this makes sense, although it would be nice to cache some of the non-essential data there. I’ve no idea if this is a HTC problem or an Android problem (I have a HTC Aria), but some Googling would seem to indicate that it’s not uncommon.

In the People app choosing View from the menu allows you to pick which sources to use to display contacts. I had 5,854 contacts from Twitter, despite having configured the Twitter app to only sync with existing contacts. I also had a bunch of Facebook contacts, with the same configuration (existing contacts).

I tried deleting Twitter from Accounts & Sync. This warned that it would remove contacts (great!) but after blowing it away Contacts Storage had more than doubled to over 70MB.

Time to go nuclear. I backed up existing contacts and then deleted all data from Contacts Storage. My phone is happy again.

Contacts and sync in general is the worst part of the Android experience. HTC Sync is a contact-duplicating, pop-up-and-wave-my-arms-in-the-air-every-time-I-do-anything piece of Adobe Air uselessness. Google really needs a better answer for people who live in Outlook on the desktop. Or maybe they’ll eventually grind me down into GMail…

Add your comment...

Related Posts

(All Marketing Posts)

Circles

Updated on Friday, February 24, 2017

 A Circle

I just got on Google+, and the Circles concept definitely moves the ball forward, but my heart sinks a little at having yet another disconnected social identity. It’s been said before, but it’s worth saying again – social networking needs to be an open, core internet standard like email. You can live on Facebook, Google, Twitter, wherever but your social graph should be independent of any specific service.

I don’t mean this in any (well, OK, a little) granola crunching open source way. Companies should compete to the death on their social graph implementation and added value. But the actual data on who your friends are should belong to you and should be both portable and interoperable. I should be able to friend someone on Google from within Facebook and share core items in both directions. If I get fed up of Facebook I should be able to move my graph and central identity elsewhere.

We’ve got OpenSocial, strangely not mentioned in the same breath as Google+, and Open Graph which is open for things but not people. Also FOAF, XUP, and other possible foundational standards. Of course the barriers here aren’t technical.

Altly wants to be Pepsi to Facebook’s Coke. I’m waiting to see what it tastes like, but it doesn’t sound like they’re itching to change the game.

Diaspora is an interesting project, but running instances (pods) of a social network is the wrong level of abstraction.

Of course ‘owning’ the graph is tremendously valuable and it’s hard to see Facebook giving this up anytime soon. If Google really don’t want to be evil they should use Google+ to liberate us from the tyranny of walled social gardens. Unless it turns out to be another Buzz or Wave in which case it’s down to us.

Add your comment...

Related Posts

(All Etc Posts)

(Published to the Fediverse as: Circles #etc #google #facebook #twitter #socialgraph #opengraph #opensocial #foaf #xup #altly #diaspora #buzz #wave Did the Google+ Circles concept revolutionize social networking, and what we should have instead. )

Reviews and links for April 2010

Updated on Friday, May 22, 2020

The Spire by Richard North Patterson

3/5

A good enough holiday read and nice to see Patterson return to a straight psychological thriller rather than the last few OpEds loosely wrapped with some plot.

 

Advanced .NET Debugging (Addison-Wesley Microsoft Technology Series) by Mario Hewardt

5/5

Comprehensive introduction to low level .NET debugging - when you need to fire up WinDbg to check out the state of the managed heap, or debug a crash dump from the field you'll find this book invaluable. I wish it had been available when I started figuring out how to use SOS.

 

The Complete Stories of J. G. Ballard by J.G. Ballard

5/5

Wonderful collection of all of Ballard's short stories. It's a huge book with surprisingly few duds. My favorites include The Illuminated Man, clearly the inspiration for The Crystal World, which includes meaning bombs like "It's almost as if a sequence of displaced but identical images were being produced by refraction through a prism, but with the element of time replacing the role of light." and The Ultimate City (which isn't using ultimate in the sense of being good...). I've read most of Ballard's novels but not many of the short stories before. They're well worth the time.

 

Links

- Microsoft Agrees With Apple And Google: “The Future Of The Web Is HTML5″ from TechCrunch (Which makes it all the more tragic that a huge number of clients will still be running IE6 :().

- Comedian criticises BBC 'rebuke' from BBC News | News Front Page | World Edition (The problem isn't that it was anti-Semitic, it's that it wasn't funny.).

- UK 'has a high early death rate' from BBC News | News Front Page | World Edition (That'll be the deep fried mars bars and chips.).

- Oklahoma, where women's rights are swept away from All Salon (Competing with AZ to be the most fucked up state? Sigh :().

- Cameras capture 'Highland tiger' from BBC News | News Front Page | World Edition (Tabbs was bigger than that (a house cat)).

- MI5 dumps staff lacking IT skills from BBC News | News Front Page | World Edition (MI5 has staff without computer skills?).

- The Internet Provides. from jwz (Disturbing).

- Who Really Spends The Most On Their Military? from Information Is Beautiful (Click through to the Guardian blog post, interesting reading.).

Add your comment...

Related Posts

(All Etc Posts)