I have been trying to obtain a html and translate the razor code in a string to send and email to multiple users. The emails are scheduled by Quartz
and send to the users.
The mail is generating a link via @Url.Action
. I notice that I don't have a Controller
nor HttpContext
at this point on my application. I have been trying a way to translate the razor
code ( RazorEngine
and MvcMailer
) to a string and sending in the email, but with no use because I cannot translate the @Url.Action
and can't find a working package MvcMailer
for Visual Studio 2017
Is there a way to possible do this?
Here is the template of the Email:
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
Here is the email generator
public MailMessage GetMessage(EmailModel notification)
{
string BodyTemplate = ReplaceEmailBody(notification);
return new MailMessage
{
To = { "testuser@testdomain.com" },
Subject = DateTime.Now.ToString(),
IsBodyHtml = true,
Body = BodyTemplate
};
}
Here is my rubbish attempt to replace the razor:
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
}
All this code is running in a execute job of Quartz
I'm using Visual Studio 2017
As @Stanislav Nedeljkovic suggested, I put the Url on a config file and manage to create and HttpContext
with it. Then I could translate the rest with RazorEngine
.
var request = new HttpRequest("/", ConfigFile.Url, "");
var response = new HttpResponse(new StringWriter());
httpContext = new HttpContext(request, response);
var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);
var urlHelper = new UrlHelper(requestContext);
var url = urlHelper.Action("Index", "Item", new { ItemId = model.itemId});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.