不确定是否有人遇到过这个问题,但我正在尝试使用MVCMailer发送电子邮件。我能够安装它并更新T4Scaffolding包,没有任何问题。
我有一个aspx页面,这是创建一个报告,我希望该报告附加到电子邮件。但是,当我转过身来调用UserMailers类中的SendReport方法时,它在PopulateBody调用中抛出一个错误,指出routeData为空
以下是我的代码
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;
}
}我得到的错误是“值不能为空,参数名: routeData”
我已经在网上找过了,没有找到任何与这个问题相关的东西,也没有找到任何遇到这个问题的人。
发布于 2012-05-10 17:19:17
它被称为Mvc Mailer是有原因的。您不能在普通的asp.net (.aspx)项目中使用它,只能在MVC项目中使用。
发布于 2015-03-13 09:50:01
正如Filip所说的那样,它不能在ASP.NET ASPX页面的代码后台中使用,因为没有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()
};
}
}当然,还有很多问题,比如安全性、协议(控制器不能访问原始页面)、错误处理--但是如果你发现自己被卡住了,这是可行的。
https://stackoverflow.com/questions/10503404
复制相似问题