【发布时间】:2017-10-10 14:16:18
【问题描述】:
我一直在尝试获取 html 并将剃须刀代码转换为字符串以发送和电子邮件给多个用户。电子邮件由Quartz 安排并发送给用户。
邮件正在通过@Url.Action 生成链接。我注意到我的应用程序此时没有Controller 或HttpContext。我一直在尝试一种将razor 代码(RazorEngine 和MvcMailer)转换为字符串并在电子邮件中发送的方法,但没有用,因为我无法翻译@Url.Action 并且找不到工作用于 Visual Studio 2017 的包 MvcMailer
有没有办法做到这一点?
这是电子邮件的模板:
Hi @ViewBag.RecipientName,
Client that buy this item is @Model.ClientName
<p> <a href='@Url.Action("Index", "Item", new { ItemId = Model.ItemId}, Request.Url.Scheme)'>Click here to check the Item</a> </p>
@ViewBag.RecipientEmailAddress
这是电子邮件生成器
public MailMessage GetMessage(EmailModel notification)
{
string BodyTemplate = ReplaceEmailBody(notification);
return new MailMessage
{
To = { "testuser@testdomain.com" },
Subject = DateTime.Now.ToString(),
IsBodyHtml = true,
Body = BodyTemplate
};
}
这是我更换剃须刀的垃圾尝试:
private string ReplaceEmailBody(EmailModel notification)
{
string notificationBody = "";
notificationBody = Templates.Resources.MailTemplate;
notificationBody = notificationBody.Replace("@ViewBag.RecipientName", notification.RecipientName);
notificationBody = notificationBody.Replace("@ViewBag.RecipientEmailAddress", notification.RecipientEmailAddress);
notificationBody = notificationBody.Replace("@Model.CLIENT_NAME", notification.ClientName);
//Need to replace the Url.Action
}
所有这些代码都在Quartz的执行作业中运行
我正在使用 Visual Studio 2017
【问题讨论】:
-
您可以向您的石英服务提供 url(mvc 控制器操作),该服务将被调用以生成剃刀电子邮件(并最终发送)。
-
@StanislavNedeljkovic 我该怎么做?
-
如果 url 是静态的,把它放在配置文件中,如果是动态的,把它作为作业参数发送。
标签: c# asp.net-mvc email razor quartz.net