1

Postal.mvc5 を使用して電子メールを送信しようとすると、「値を null にすることはできません。パラメータ名: httpContext」というエラー メッセージが表示されました。

このエラーは、Postal.EmailService.CreateMailMessage(Email email) を呼び出そうとしたときに発生しました。

これについて誰でも助けることができますか?ありがとう。

以下、エラー詳細です。

at System.Web.HttpContextWrapper..ctor(HttpContext httpContext)
at System.Web.Optimization.Styles.get_Context()
at System.Web.Optimization.Styles.get_Manager()
at System.Web.Optimization.Styles.RenderFormat(String tagFormat, String[] paths)
at System.Web.Optimization.Styles.Render(String[] paths)
at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\XXX\Views\Shared\_Layout.cshtml:line 19
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Postal.EmailViewRenderer.RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder)
at Postal.EmailViewRenderer.Render(Email email, String viewName)
at Postal.EmailService.CreateMailMessage(Email email)

ForgotPassword トリガーは、メール ヘルパー経由でメールを送信します。

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.Email);
            if (user == null)
            {
                // Don't reveal that the user does not exist or is not confirmed
                return View("ForgotPasswordConfirmation");
            }

            // Check if email confirm required
            if (CacheHelper.Settings.EmailConfirmedRequired && !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
                return View("ForgotPasswordConfirmation");
            }                

            string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

            var emailTemplateQuery = await _emailTemplateService.Query(x => x.Slug.ToLower() == "forgotpassword").SelectAsync();
            var emailTemplate = emailTemplateQuery.Single();

            dynamic email = new Postal.Email("Email");
            email.To = CacheHelper.Settings.EmailContact;
            email.From = CacheHelper.Settings.EmailContact;
            email.Subject = emailTemplate.Subject;
            email.Body = emailTemplate.Body;
            email.CallbackUrl = callbackUrl;
            EmailHelper.SendEmail(email);

            return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

メール ヘルパー クラス: EmailService.CreateMailMessage(email) でエラーが発生しました。

        public static IEmailService EmailService = Postal.Email.CreateEmailService();

    public static void SendEmail(Email email, bool preMailer = true)
    {

        Task.Factory.StartNew(() =>
        {
            try
            {
                //skip email if there is no settings
                if (string.IsNullOrEmpty(CacheHelper.Settings.SmtpHost) && string.IsNullOrEmpty(CacheHelper.Settings.SmtpPassword))
                    return;

                //Error encountered when call create mail message
                var message = EmailService.CreateMailMessage(email);

                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.UseDefaultCredentials = false;

                    // set credential if there is one
                    if (!string.IsNullOrEmpty(CacheHelper.Settings.SmtpUserName) && !string.IsNullOrEmpty(CacheHelper.Settings.SmtpPassword))
                    {
                        var credential = new NetworkCredential
                        {
                            UserName = CacheHelper.Settings.SmtpUserName,
                            Password = CacheHelper.Settings.SmtpPassword
                        };
                        smtpClient.Credentials = credential;
                    }
                    smtpClient.Host = CacheHelper.Settings.SmtpHost;
                    smtpClient.EnableSsl = CacheHelper.Settings.SmtpSSL;

                    if (CacheHelper.Settings.SmtpPort.HasValue)
                        smtpClient.Port = CacheHelper.Settings.SmtpPort.Value;

                    //moving CSS to inline style attributes, to gain maximum E-mail client compatibility.
                    if (preMailer)
                        message.Body = PreMailer.Net.PreMailer.MoveCssInline(message.Body).Html;

                    smtpClient.Send(message);
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
        });
    }
4

1 に答える 1