Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to send email through List Event Receiver?

$
0
0

We would like to send an Email notification through Custom List Event Receiver [ItemAdded Event]. Does the below code suffice the requirment?

//below Code will be placed in ItemAdded Event

 if (properties.ListItem["PeoplePickerFieldEmail"] != null)
                            {
                                SPFieldLookupValue lookupFieldEmail = new SPFieldLookupValue(properties.ListItem["PeoplePickerFieldEmail"].ToString());
                                string TechDeveloperEmail = lookupFieldEmail.LookupValue.ToString();

                                if (!string.IsNullOrEmpty(TechDeveloperEmail))
                                {
                                    string toField = TechDeveloperEmail;
                                    string subject = "Business Approval for " + properties.AfterProperties["Title"] + " has been Completed.";
                                    string body = "Business Approval for " + properties.AfterProperties["Title"] + " has been Completed. Please start the Technical Details.";
                                    HttpContext oldContext = HttpContext.Current;
                                    HttpContext.Current = null;
                                    bool success = SPUtility.SendEmail(thisWeb, true, true, toField, subject, body);
                                    HttpContext.Current = oldContext;
                                }
                            }

Findings:

And event receiver runs in the context of an HTTP request. It is known that SPUtility.SendEmail has issues with this. A common practice is to set HttpContext.Current to null while sending the email:

Reference link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail(v=office.12).aspx


Amalaraja Fernando,
SharePoint Architect
Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you. This post is provided "AS IS" with no warrenties and confers no rights.


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>