Blog of an overweight SharePoint addict

Thu 29 May 08

Using the undocumented SPUtility.SendEmail method.

Filed under: General, Programming, SharePoint, Visual Studio 2005 — Reginald @ 3:31 pm

As with a lot of the sharePoint API, the SPUtility class and more importantly the sendEMail method lacks a decent amount of documentation on its use.

After a little bit of playing around with a console session, I’ve found what seems to be the best way to use this function. (For me at least.. feel free to disagree!!)

Requires:
using System.Collections.Specialized;

For the purposes of my test code, I used a console sessions and wrapped the send email process inside two using statements to populate site and web with the test sharepoint site.


StringDictionary headers = new StringDictionary();
headers.add("to","reginald@myfatblog.co.uk");
headers.add("cc","thisiscopy@myfatblog.co.uk");
headers.add("bcc","thisistheblindcopy@myfatblog.co.uk");
headers.add("from","MySharePointEmail@myfatblog.co.uk");
headers.add("subject","How to use SendEMail from SPUtility");
headers.add("content-type","text/html"); //This is the default type, so isn't neccessary.

string bodyText ="This is the body of my email, in html format.";

SPUtility.SendEmail(web, headers, bodyText);

The SendEmail method makes it very easy to pop emails out using your SharePoint sites current SMTP configuration, the only downside is that this does not allow you to use attachments. If you need to do this, then you’ll need to look down the .Net mail methods.. (I found a decent post on David Fekkes blog on how to do this using the .Net methods. I haven’t tried it yet, but it’s a good starting point. Email with attachments from SharePoint code.

5 Comments »

  1. Hi. I have an excel workbook that uses VB to draw boxes representing the start time and end time of events. This provides a visual depiction of a scheduled flow. Is there a way to upload that VB into sharepoint (WSS or MOSS) and use a list that contains the same data, and draw the boxes? I can send the spreadsheet it that helps.

    Thank you

    Comment by ratzelda — Tue 24 Jun 08 @ 9:23 am

  2. Hi Ratzelda,

    You haven’t stated what version of excel you’re using, but I’m assuming it’s Excel 2003, in which case the code is written in VBA which wouldn’t work in SharePoint as it’s .Net based.

    If you wanted to re-create a similar effect you’d probably need to look at creating a webPart that would reference the list data and process it in a similar manner. Wether or not you could translate that into an HTML output or if you’d have to use a workspace to drop system.drawing info into I’m not sure. It’s not something I’ve tried in WSS.

    Reg.

    Comment by Reginald — Wed 25 Jun 08 @ 8:24 pm

  3. Hello!

    Is it possible to set not only the e-mail address in the from header but the name too? So that the from would look like this:
    Name[addr@something.com]

    like with MailMessage:

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress( “addr@something.com”, “Name of Company”);

    Thanks,
    Tamás

    Comment by Tomash — Wed 15 Oct 08 @ 1:42 pm

  4. Hi Tamás,

    Unfortunately the SPUtility.SendEmail isn’t very well documented (Hence the article title!), so I’m not sure how it would handle the format you propose.

    It’s meant to be a fairly simple implementation of SendMail so I wouldn’t imagine it will like recieving headers that don’t comply to the SMTP RFC.

    Reg.

    Comment by Reginald — Fri 17 Oct 08 @ 1:55 pm

  5. [...] headers = new StringDictionary(); headers.add("to","reginald@myfatblog.co.uk"); headers.add("cc","thisiscopy@myfatblog.co.uk"); [...]

    Pingback by Using the undocumented SPUtility.SendEmail method. « Blog of an overweight SharePoint addict « The Ramblings of Ben — Tue 9 Mar 10 @ 10:41 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress