Sharepoint Send Email workaround via WCF Service

When you try to send an email with classical way in WCF service which is served in sharepoint you probably getting an error. As a workaround, you have to set HttpContext.Current = null. it will use the right context .

Here is the example:

try {    using (SPSite site = new SPSite("http://blog.bugrapostaci.com"))    {        SPWeb web = site.RootWeb;         {             //Save context to temp variable than set null             HttpContext tempContext = HttpContext.Current;             HttpContext.Current = null;             string MailTo = "admin@blog.bugrapostaci.com";             string Subject = "Make it easy";             string Body = "A message from Bugra";             bool success = SPUtility.SendEmail(web, true, true, MailTo, Subject, Body);             HttpContext.Current = tempContext;         }     } } catch (Exception ex) {     // Exception Handling }
Happy coddings...

 

Advertisements