Sputility.SendEmail은 ASMX에서 호출 할 때 FALSE를 반환합니다
문제
이메일을 보내야하는 ASMX 페이지 내에 기능이 있지만 SPUtility.SendEmail
는 false를 반환합니다.SPContext.Current
가 null이므로 사이트 URL이있는 SPSite
객체를 만들고, 그로부터 rootweb를 얻을 수 있습니다. 이는 SPUtility.SendEmail
에 전달합니다.POSTACKODICETAGCODE를 사용 하여이 정확한 코드 (도우미 함수에 캡 칩 함)를 호출하면 작동합니다.두 호출에서 SPContext.Current.Web
객체를 검사하면 동일합니다.
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;
}
}
제휴하지 않습니다 sharepoint.stackexchange