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

About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: