Sending email through GMail (or Google apps) with C#

May 29, 2009 05:18 by bjones

I have a couple of websites I've built that use GoDaddy hosting.  Instead of paying GoDaddy for their email service for my domains, I use Google Apps. It's free, they keep increasing the amount of storage (7GBs as of this blog) and I like the search feature.  In addition, I don't want to have my emails bounce as SPAM because I'm relaying it off GoDaddy's open relay so I'm bouncing it off of smtp.google.com.  Don't forget to add an SPF record (maybe I'll make that part 2 of this article).

I've been using System.Net.Mail to bounce email off a local SMTP host but had to go back to System.Web.Mail in order to authenticate for Google (my tin foil hat personality would suggest that Microsoft deprecated System.Web.Mail and left out authentication on System.Net.Mail to stop us from using Gmail). 

Below is a function I use to send email through GMail in my C# web apps.  The original code was written by Alex Sanchez for bouncing it off your local (or remote) mail server without authentication; I bastardized it to add authentication.


I have a typo in the Param section for the inFrom.  I couldn't figure out how to excape a < or > so I had to leave them out.  Make sure you use this format for your inFrom: "Name To Display" <email@domain.com>.  

Here you go: 

[code:c#]

/// <summary>
/// Sends email using Gmail or Email for Google apps.
/// </summary>
/// <param name="inFrom">From email addresses - format is: "Name To Display" email@domain.com </param>
/// <param name="inTo">To email addresses</param>
/// <param name="inSubject">Subject line</param>
/// <param name="inBody">Body of email in html markup or plain text</param>
/// <param name="inAttachmentPaths">String array of attachment paths on webserver</param>
/// <param name="isHTMLFormat">1 = HTML Format, 2 = plain text</param>
/// <param name="gmailAccountName">Account name for Gmail.  If you are using Email for Google Apps, you need to supply full email: email@domain.com</param>
/// <param name="gmailPassword">Password for your gmail account</param>
public static void SendEmailViaGmail(string inFrom, string inTo, string inSubject, string inBody, string[] inAttachmentPaths, Boolean isHTMLFormat, string gmailAccountName, string gmailPassword)
{
    System.Web.Mail.MailMessage myMailMessage = null;
   
    // Build email message
    myMailMessage = new System.Web.Mail.MailMessage();
    myMailMessage.From = inFrom;  // Format is: "Name To Display" <email@domain.com>
    myMailMessage.To = inTo;
    myMailMessage.Subject = inSubject;
    myMailMessage.Body = inBody; 
   
    if (isHTMLFormat)  // I send in plain text for SMS messages, HTML for the rest of the world
    {
        myMailMessage.BodyFormat = MailFormat.Html;
    }
    else
    {
        myMailMessage.BodyFormat = MailFormat.Text;
    }
   
   
    // Add attachments
    if (inAttachmentPaths != null)
        for (int i = 0; i < inAttachmentPaths.Length; i++)
            myMailMessage.Attachments.Add(new Attachment(inAttachmentPaths[i]));
   
   
    //  Authenticate
    myMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
    // Username for gmail - email@domain.com for email for Google Apps
    myMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", gmailAccountName);
    // Password for gmail account
    myMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", gmailPassword);
    // Google says to use 465 or 587.  I don't get an answer on 587 and 465 works - YMMV
    myMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
    // STARTTLS
    myMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
   
   
   
   
    // assign outgoing gmail server
    SmtpMail.SmtpServer = "smtp.gmail.com";
    SmtpMail.Send(myMailMessage);
}

[/code]


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: SysAdmin | .net | Code Monkey
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Fizzbin

May 1, 2009 10:56 by bjones
This post is only for geeks - you know the guys that live to sit in front of two (or three or better yet, four!) monitors pouring over a stack trace to find out why "is null" and "= null" aren't the same. Or for you sysadmins spending 2 days checking 5,000 cable drops to find out who created a loopback with the $5 linksys hub that brought down your entire network. If you fit one of these categories then fizzbin to you!

Imagine this scenario: You spent the last 16 straight hours working on a 52 node Beowulf cluster and you decide to go home while the final node is coming up. "I'll check it at home," you think to yourself. You get home, down a bottle of bawls and fire up your Macbook to find out that your internet connection is down. You get an IP, ping to your router but nothing further. Dang it, sun spots are wreaking habit again so you pick up the phone and call your ISP. Level 1 support gets on and starts asking you if you turned on your computer and is there a light on your modem? There is no need to talk to level 1 but they won't transfer you until you pretend to disconnect your wireless router and plug your computer (that you lied and said was XP so they would help) directly in to the cable modem. Reboot, wait, unplug cables, wait, unplug modems, wait, etc...

It's time to put an end to this frustration. Yes, my Mom needs to go through each step but, we geeks don't. Scott Hanselman has come up with an ingenious way for us to wink at the support tech on the other end of the phone and get us on to some real troubleshooting on their end. Fizzbin! It's a secret codeword that we can use to get past the mundane troubleshooting. It's so simple to use, just say "fizzbin" and the level 1 techie will know that you've already done everything she is going to asking you. And everything her level 2 techie is going to ask you so you both stop wasting each other's time. It will only work if we all know it, so pass it on!

Fizzbin to you!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: SysAdmin
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Stop the racism surrounding the H1N1 virus (swine flu)

May 1, 2009 08:07 by bjones
Michael Savage, a talk show host, said to avoid "contact anywhere with an illegal alien... and that starts with restaurants... don't know if they wipe their behinds with their hands!" Wait, seriously? I guess I have it all wrong, only illegal aliens don’t wash their hands. And only illegal aliens can spread the disease.

Frankly, I'm appalled with the racism surrounding the H1N1 virus! I think people are scared of the virus and they are too emotionally stunted to express themselves so the only thing they can do is conjure up hatred. People are using this outbreak as a way to promote their racist agenda. I mean, who is going to complain about a racist comment here or there about the supposed dirty people that gave us this flu?

Hispanic people are not dirty, they are not sub-Human and they are not all illegal aliens. Let people know this kind of racism disguised as stupidity is unacceptable (here is a list of stations that stream Michaels Savage's radio show).

When need to work together to get past the H1N1 virus, not against each other.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: op ed
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed