【问题标题】:Cannot send mail in SharePoint using Event Receiver无法使用事件接收器在 SharePoint 中发送邮件
【发布时间】:2012-04-20 16:50:12
【问题描述】:

我在 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)

感谢您的帮助。

【问题讨论】:

  • 您是否遇到异常或 SharePoint ULS 日志中有错误?

标签: c# sharepoint events email


【解决方案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;

参考(向下滚动到 cmets):http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail(v=office.12).aspx

【讨论】:

  • 谢谢,我试过了,还是没有解决问题。是否还有关于事件接收器和在 SharePoint 中发送邮件的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2012-02-07
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多