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: Code Monkey | SysAdmin | .net
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Comments