Paul's profilePaul Galvin's SharePoint...BlogListsSkyDrive Tools Help

Blog


    October 27

    Quick and Easy: Send Email Using Gmail SMTP Server in .NET C#

    This isn’t exactly a new topic, but when I needed to do it, I found a lot of “why won’t this work for me” and not too many direct answers.  I hope someone finds this useful.

    The following bit of code will send an email using my own gmail account to do it, including attachments:

    using System.Net.Mail;
    using System.Net;
    NetworkCredential loginInfo = new NetworkCredential("[My Gmail ID]", "[My Gmail Password]");
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("[M Gmail Id]@gmail.com");
    msg.To.Add(new MailAddress("paul.galvin@arcovis.com"));
    msg.Subject = "Test infopath dev subject";
    msg.Body = "<html><body><strong>A strong message.</strong></body></html>";
    msg.IsBodyHtml = true;
    
    foreach (string aFile in NIPFD.GetAttachmentNamesAndLocations())
    {
        msg.Attachments.Add(new Attachment(aFile));
    } // Adding attachments.
    
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = loginInfo;
    client.Port = 587;
    client.EnableSsl = true;
    client.Send(msg);

     

    A few key bits that slowed me down and other observations / notes:

    • The first line that creates the loginInfo object needs to use the gmail ID stripped of “@gmail.com”.  So, if my gmail email address is “sharepoint@gmail.com” and my password is “xyzzy” then the line would look like:

    NetworkCredential loginInfo = new NetworkCredential("sharepoint", "xyzzy");

    • My gmail account is set up to use SSL and that wasn’t a problem.
    • There is some conflicting information out there on what port to use.  I used port 587 and it worked fine for me.
    • In my case, I also needed to send attachments.  That NIPFD object has a method that knows where my attachments are.  It’s returning a fully path (e.g. “c:\temp\attachment1.jpg”.  In my test, I had two attachments and they both worked fine.

    I used visual studio 2008 to write this code.

    </end>

    Subscribe to my blog.

    Follow me on Twitter at http://www.twitter.com/pagalvin

    Technorati Tags: ,,,

    Comments (2)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Paul Galvin has turned off comments on this page.
    Balaji Kwrote:
    Clean and simple...

    Thanks for sharing.
    Nov. 12
    Jeffwrote:
    The PowerShell Team blog recently had a similar post that I found invaluable as a server admin. Visual Studio is great but many of my peers are not developers and don't have a license purchased. PowerShell feels more like the command line and is more accessible to them. Just a suggestion for any admins out there.

    Best,
    Jeff (www.spjeff.com / @spjeff)

    Sending Automated emails with Send-MailMessage

    http://blogs.msdn.com/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx
    Nov. 2

    Trackbacks

    The trackback URL for this entry is:
    http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!4935.trak
    Weblogs that reference this entry
    • None