Posted at: 1:49 PM on 29 December 2009 by Muhimbi
One of the most common (sub) tasks executed from a SharePoint workflow is the submission of an email. SharePoint 2007 ships with an Email Workflow Action out of the box, which is unfortunately very limited in its abilities. For example, it is not possible to include attachments, specify the From Address, set the Priority or specify the SMTP server to use.
In this blog post I’ll describe how to send an email from a workflow that automatically includes all files / attachments for the current item from a SharePoint Designer workflow using the Workflow Power Pack.
A quick introduction for those not familiar with the product: The Muhimbi Workflow Power Pack for SharePoint allows custom C# or VB.NET code to be embedded in SharePoint Designer Workflows without the need to resort to complex Visual Studio based workflows, the development of bespoke Workflow Activities or long development cycles.
The solution presented below uses the standard Build Dynamic String Action to create the body of the email. This body is then passed as Parameter 1 to the Execute Custom Code Action. Parameter 2 is used to specify the To Address. This code is a great starting point for further customisation. For more details see the standard .net MailMessage and SmtpClient classes.
Create the workflow as follows:
- Download and install the Muhimbi Workflow Power Pack for SharePoint.
- Make sure you have the appropriate privileges to create workflows on a site collection.
- Create a new workflow using SharePoint Designer.
- On the Workflow definition screen associate the workflow with the list or library of your choice, tick the boxes next to both ‘Automatically start… ’ items and proceed to the next screen.
- Click the Actions button and insert the Build Dynamic String action.
- Click dynamic string and enter an HTML based email.
- Click variable1 and create a new string based variable named mailMessage.
- Click the Actions button and insert the Execute Custom Code action.
- Click parameter 1, open the Workflow Lookup dialog and select Source: Workflow Data, Field: mailMessage.
- Click parameter 2 end enter the address to send the email to. Naturally this could be a lookup value as well.
- Insert the following C# based code by clicking this code.
string smtpServer = MyWorkflow.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
SPListItem item = MyWorkflow.Item;
// ** Get the standard sender address for the Web App.
// ** Feel free to replace with an address of your choice.
string from = MyWorkflow.Site.WebApplication.OutboundMailSenderAddress;
string to = (string) MyWorkflow.Parameter2;
string subject = "New SharePoint Files";
string body = (string) MyWorkflow.Parameter1;
// ** Process any 'workflow variables' that may exist in the body.
body = Helper.ProcessStringField(body, MyWorkflow.ActivityExecutionContext.Activity, null);
MailMessage message = new MailMessage(from, to, subject, body);
message.IsBodyHtml = true;
message.Priority = MailPriority.Normal;
// ** Add optional CCs and BCCs
// ** Do we need to attach a single file or a list of files attached to a list item?
if (item.File != null)
// ** Attach the file itself
Stream attachmentStream = new MemoryStream(item.File.OpenBinary());
Attachment attachment = new Attachment(attachmentStream, item.File.Name);
// ** Attach all files that are part of the list item
foreach (string fileName in item.Attachments)
SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + fileName);
Stream attachmentStream = new MemoryStream(file.OpenBinary());
Attachment attachment = new Attachment(attachmentStream, file.Name);
// ** Send the email
SmtpClient client = new SmtpClient(smtpServer);
- Close the Workflow Designer and add an item to your list or library to trigger the workflow.
The code in this example is a great starting point for making further customisations. You may want to change which parameters are passed in or load the attachments from a different item or even using an HTTP Request. The solution presented in this post also works very well when combined with one of our other postings: Automatically convert files to PDF using an e-mail enabled SharePoint Document Library