MVCMailer的问题

MVCMailer的问题,mvcmailer,Mvcmailer,不确定是否有其他人遇到过这个问题,但我正在尝试使用MVCMailer发送电子邮件。我能够安装并更新T4脚手架包,没有任何问题 我有一个正在创建报告的aspx页面,我希望将该报告附加到电子邮件中。但是,当我在UserMailers类中调用SendReport方法时,它在PopulateBody调用中抛出一个错误,表示routeData为null 这是我的密码 public class UserMailer : MailerBase, IUserMailer { /// <summar

不确定是否有其他人遇到过这个问题,但我正在尝试使用MVCMailer发送电子邮件。我能够安装并更新T4脚手架包,没有任何问题

我有一个正在创建报告的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;
    }
}
public类UserMailer:MailerBase,IUserMailer
{
/// 
///使用此方法发送电子邮件报告
/// 
///要发送到的地址。
///视图的名称。
///邮件消息
public MailMessage SendReport(字符串到地址,字符串视图名称)
{
var message=新邮件{Subject=“Report Mail”};
message.To.Add(toAddress);
ViewBag.Name=“Testing-123”;
this.PopulateBody(mailMessage:message,viewName:“SendReport”);
返回消息;
}
}
我得到的错误是“值不能为null。参数名称:RoutedData”


我在网上搜索过,没有发现任何与此问题相关的内容,也没有找到任何遇到此问题的人

之所以称之为MvcMailer是有原因的。
您不能在普通asp.net(.aspx)项目中使用它,只能在MVC项目中使用。

正如Filip所说,它不能在asp.net aspx页面的codebehind中使用,因为没有
ControllerContext
/
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()
        };
    }
}

当然,还有很多问题,比如安全性、协议(控制器将无法访问原始页面)、错误处理-但是如果您发现自己陷入困境,这是可行的。

Filip-请指出我的帖子中指出该项目不是MVC项目的地方?有很多人在MVC项目中使用web表单的例子。我错了,我错了:)不过,问题仍然存在,如果你调用.aspx webform,路由不会加载。我也遇到了这个问题,但奇怪的是,只有一两封我们发送的电子邮件,你有没有找到问题的根源?嗨@George,你找到解决这个问题的方法了吗?我面临着同样的问题,尽管我使用的是MVC控制器