我在SharePoint 2010中有一个Web部件,它有一个事件接收器,当项目添加到列表时会触发。事件接收器应该发送邮件但不发送。
我在没有事件接收器的情况下尝试时工作正常,如何使用事件接收器发送邮件?
StringDictionary headers = new StringDictionary();
string body = "Hi!";
headers.Add("to", "paulo@paulo.se");
headers.Add("from", "paulosaysno@paulo.se");
headers.Add("subject", "Paulo says hi");
headers.Add("content-type", "text/html");
SPUtility.SendEmail(web, headers, body)
感谢您的帮助。
答案 0 :(得分:1)
事件接收器在HTTP请求的上下文中运行。众所周知,SPUtility.SendEmail存在此问题。通常的做法是在发送电子邮件时将HttpContext.Current设置为null:
SPWeb thisWeb = thisSite.RootWeb;
string toField = "someone@microsoft.com";
string subject = "Test Message";
string body = "Message sent from SharePoint";
HttpContext oldContext = HttpContext.Current;
HttpContext.Current = null;
bool success = SPUtility.SendEmail(thisWeb, true, true, toField, subject, body);
HttpContext.Current = oldContext;
参考(向下滚动到评论):http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail(v=office.12).aspx