«

»

May 29

Using the undocumented SPUtility.SendEmail method.

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.

4 comments

3 pings

Skip to comment form

  1. ratzelda

    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

  2. Cimares

    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.

  3. Tomash

    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

  4. Cimares

    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.

  1. Using the undocumented SPUtility.SendEmail method. « Blog of an overweight SharePoint addict « The Ramblings of Ben

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

  2. SharePoint 2007 Email SPUtility.SendMail() Guide « The Ramblings of Ben

    […] Reference Link : http://www.myfatblog.co.uk/?p=79 […]

  3. Testing SharePoint outbound e-mail - Blog of an overweight SharePoint addict

    […] This is an update of an earlier blog post that I wrote about using the SPUtility class and the SendEmail method, however instead of doing this in code, we’re going to use PowerShell. (This is the original blog post.) […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.