2

他の誰かがこの問題に遭遇したかどうかはわかりませんが、MVCMailer を使用してメールを送信しようとしています。問題なく T4Scaffolding パッケージをインストールして更新することができました。

レポートを作成する aspx ページがあり、そのレポートを電子メールに添付したいと考えています。ただし、振り返って UserMailers クラスの SendReport メソッドを呼び出すと、PopulateBody 呼び出しで routeData が null であるというエラーがスローされます。

これが私のコードです

public class UserMailer : MailerBase, IUserMailer
{
    /// <summary>
    /// Email Reports using this method
    /// </summary>
    /// <param name="toAddress">The address to send to.</param>
    /// <param name="viewName">The name of the view.</param>
    /// <returns>The mail message</returns>
    public MailMessage SendReport(string toAddress, string viewName)
    {
        var message = new MailMessage { Subject = "Report Mail" };
        message.To.Add(toAddress);

        ViewBag.Name = "Testing-123";

        this.PopulateBody(mailMessage: message, viewName: "SendReport");

        return message;
    }
}

私が得るエラーは「値はnullにすることはできません。パラメータ名:routeData」です

私はオンラインで調べましたが、この問題に関連するものや、この問題に遭遇した人は見つかりませんでした。

4

2 に答える 2

2

それは理由からMvcメーラーと呼ばれています。通常のasp.net(.aspx)プロジェクトでは使用できず、MVCプロジェクトでのみ使用できます。

于 2012-05-10T09:19:16.997 に答える
0

ControllerContextフィリップが言ったように、 /がないため、ASP.NET ASPX ページの分離コード内では使用できませんRequestContext

私にとって最も簡単な方法は、コントローラー アクションを作成し、それを使用WebClientして ASPX ページから http 要求を作成することでした。

    protected void Button1_Click(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();

        var sendEmailUrl = "https://" + Request.Url.Host + 
                           Page.ResolveUrl("~/email/SendGenericEmail") + 
                           "?emailAddress=email@example.com" + "&template=Template1";

        wc.DownloadData(sendEmailUrl);
    }

それから私は単純なコントローラーを持っています

public class EmailController : Controller
{
    public ActionResult SendGenericEmail(string emailAddress, string template)
    {
        // send email
        GenericMailer mailer = new GenericMailer();

        switch (template)
        {
            case "Template1":

                var email = mailer.GenericEmail(emailAddress, "Email Subject");
                email.Send(mailer.SmtpClient);
                break;

            default:
                throw new ApplicationException("Template " + template + " not handled");
        }

        return new ContentResult()
        {
            Content = DateTime.Now.ToString()
        };
    }
}

もちろん、セキュリティ、プロトコル (コントローラーが元のページにアクセスできない)、エラー処理など、多くの問題がありますが、行き詰まった場合は、これでうまくいく可能性があります。

于 2015-03-13T01:50:00.560 に答える