Send Email in Sandbox Solution


In sandbox solution SPUtility.SendMail method has been blocked explicitly. So there is no way to send email using code in Sandbox solution.


I googled alot but not able to find any solution. Then I decided to try something and it worked :)


  • What I did was i created Custom Content Type using Visual Studio. 
  • Then created a Send Email Workflow Activity on my custom Content Type using Sharepoint Designer.
  • I saved that workflow as template and I exported that workflow to local Drive.
  • And then created a new Project using template "Import SharePoint Solution Package".
  • Then created another list programmatically using my custom content type.
  • Then associated my workflow with my custom list.
  • And then when ever I require to send an email. I just add records in my list. Workflow triggers and sends an email.
Steps in detail are as below:
  1. Create Empty SharePoint Project in Visual Studio. Create custom content type "EmailHelperContentType" with following custom FieldsEmailTo (single line)EmailCC (Single Line)EmailBodyText (MultiLine)EmailSubjectText (Single Line)
  2. Deploy this project.
  3. Open SharePoint Designer and open sharePoint Root web.Note: Always create Reusable Workflow at top level site.
  4. Click Reusable Workflow.
  5. On click will open up another window. Give Name and in Content type select your content type deployed in step 1.
  6. Under Actions > Core Actions > Send an Email
  7. Click "these users"
  8. For To field value click lookup > double click "Workflow Lookup for a user"
  9. Select Datasource = "Current Item"Field from source = "EmailTo"

  10. Similarly define CC, Subject and body as shown below.
  11. Click Save.
  12. Click workflow under Navigation. Select your Resuable Workflow. And in Ribbon click "Save as Template". The template will be saved in Site Assets". Refresh if you can't see it there.
  13. Select file and in Ribbon click "Export File". And save this file to desktop.
  14. Now Open Visual Studio and create new Project using template "Import SharePoint Solution Package"
  15. On next screen enter your site url and deploy as Sandbox as shown below. Click Next.
  16. Select the template created in step 12. And click Next.
  17. On next screen two items will shown up select both and click finish.
  18. You will notice one feature create for you.
  19. Add new Event receiver to feature
  20. In feature Activated Create new list " EmailHelper" and add Workflow association with list "EmailHelper" as show below.
string contentTypeName = "EmailHelperContentType";
            string workflowName = "Email Sender";
            string workflowTemplateName = "EmailHelperWorkflow";
           
            string listTaskName = "Tasks";
            string listHistoryName = "WorkflowHistory";
            string listEmailName = "EmailHelper";
            SPContentType siteContentType = web.Site.RootWeb.ContentTypes[contentTypeName];
            SPList historylist = web.Lists.TryGetList(listHistoryName);
            if (historylist == null)
            {
                web.Lists.Add(listHistoryName, listHistoryName, SPListTemplateType.WorkflowHistory);
                historylist = web.Lists.TryGetList(listHistoryName);
            }
            SPList tasklist = web.Lists.TryGetList(listTaskName);
            if (tasklist == null)
            {
                web.Lists.Add(listTaskName, listTaskName, SPListTemplateType.Tasks);
                tasklist = web.Lists.TryGetList(listTaskName);
            }
            SPList mainlist = web.Lists.TryGetList(listEmailName);
            if (mainlist == null)
            {
                web.Lists.Add(listEmailName, listEmailName, SPListTemplateType.GenericList);
                mainlist = web.Lists.TryGetList(listEmailName);
                mainlist.Hidden = true;
                mainlist.ContentTypesEnabled = true;
                mainlist.ContentTypes.Add(siteContentType);
                mainlist.ContentTypes.Delete(mainlist.ContentTypes["Item"].Id);
                SPWorkflowTemplate workflowTemplate = null;
                foreach (SPWorkflowTemplate template in web.WorkflowTemplates)
                {
                    workflowTemplate = template;
                    // We'll take a template everyone has.
                    if (workflowTemplate.Name == workflowTemplateName) break;
                }
                SPWorkflowAssociation association =
                   SPWorkflowAssociation.CreateListContentTypeAssociation(workflowTemplate,
                                                                          workflowName,
                                                                          tasklist,
                                                                          historylist);
                association.AllowManual = true;
                association.AutoStartCreate = true;
                association.AutoStartChange = false;
                mainlist.WorkflowAssociations.Add(association);
                mainlist.Update();

For more detail on Sandbox Solution Visit Here

Comments