質問

電子メールを送信する必要があるASMXページ内に関数を持っていますが、SPUtility.SendEmailはfalseを返します。SPContext.CurrentはNULLですので、サイトのURLを使用してSPSiteオブジェクトを作成し、そこからrootwebを取得します。これは、SPUtility.SendEmailに渡すものです。Postackの結果として、この正確なコード(ヘルパー関数にカプセル化されている)を呼び出す場合は、SPContext.Current.Webを使用します。両方の呼び出しからSPWebオブジェクトを調べると、同じように見えます。

if (string.IsNullOrEmpty(siteUrl))
    success = SPUtility.SendEmail(SPContext.Current.Web, headers, message);
else
{
    using (SPSite site = new SPSite(siteUrl))
    {
        success = SPUtility.SendEmail(site.RootWeb, headers, message);
    }
}
.

任意のアイデアメールを送信する方法?

役に立ちましたか?

解決

This is not an ideal solution, however if your code works when there is an SPContext, you can create a fake SPContext when it does not exist:

using (var site = new SPSite("url"))
{
    using (var web = site.OpenWeb())
    {
        HttpContext newContext = null;
        if (HttpContext.Current == null)
        {
            var request = new HttpRequest("", web.Url, "");
            newContext = new HttpContext(request, new HttpResponse(TextWriter.Null));
            HttpContext.Current = newContext;
        }
        HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
        HttpContext.Current.Items["HttpHandlerSPSite"] = site;

        // Do your code here

        if (newContent != null)
            HttpContext.Current = null;
    }
}
ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top