Sending Email In C# Through Gmail

by Prasanna Vignesh 13. November 2007 16:02


I've recently switched all of my email over to Google Apps. One side effect of that is that all of my applications had to be updated to send email through the Google servers rather than the email servers I was using.

I was actually able to do it with only one minor code change and a change to the web.config. First, the web.config mail settings section:

<system.net><mailSettings>
<smtp from="user@domain.com">
<network host="smtp.gmail.com" port="587" userName="user@domain.com" password="pwd" />
</smtp>
</mailSettings>
</system.net>
 
Obviously, substitute your own email address and password, but once
you've done that, this configuration should work. One shortcoming I see
with the configuration is that it doesn't allow us to specify the use
of SSL, which Gmail requires. So that's the one code change:  
 
   1:  System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
   2:  client.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["MailSSL"]);
 

To shield myself from code changes in the future, I added an app setting to determine if I want SSL. That way, in the future, if I move back to a non-Gmail account to send mail from, I just change the mailSettings section, change MailSSL to be false, and I'm good to go.

Just a note that I did try port 465 (another port supported by Gmail) to send mail initially, and got a timeout every time, so I switched to 587, and mail started going through just fine.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:

C# | Dot Net

Comments

3/16/2009 10:49:48 PM #

busby seo test

thank you for the tips on how sending email through Gmail

busby seo test United States |

9/10/2009 6:41:23 PM #

Online Loan

Browsing a few blogengine.net blogs - seems a great platform. Best wishes with your blog, cheers

Online Loan United States |

10/11/2009 4:28:16 PM #

Willis

In my opinion, it is a false way.

Willis United States |

10/12/2009 5:44:10 PM #

cunning linguist

I want my 10 seconds back

cunning linguist Germany |

11/4/2009 1:07:15 AM #

Buyer Keywords Generator

who uses this now ?

Buyer Keywords Generator United States |

11/16/2009 9:37:44 PM #

anti-aging skin care

No one is using this now.

anti-aging skin care United States |

11/16/2009 9:54:33 PM #

anti-aging skin care

I dont support it.

anti-aging skin care United States |

11/16/2009 10:14:11 PM #

antiaging

Thanks a lot to sharing with us your valuable comments

antiaging United States |

12/30/2009 6:35:42 PM #

Aloe Vera Gel

Great post! I am just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case.

Aloe Vera Gel United States |

1/1/2010 10:05:43 PM #

Ottawa Green House Cleaners

Everything you point out here are the same reason why I love gmail and have been using it ever sinceSmile

Ottawa Green House Cleaners Canada |

1/6/2010 12:14:33 PM #

Chronic fatigue syndrome symptoms

Thanks for sharing your views on the topic. It makes one think and look the other side of the story. http://www.postviralfatigue.me.uk/symptoms.aspx

Chronic fatigue syndrome symptoms United States |

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen, Modified by Prasanna Vignesh.



About Me

Prasanna Vignesh Prasanna Vignesh
Mobile application developer at ZSL. More...



RecentPosts

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar