2

I'm new with ASP.NET Core and I am trying to make emails, based on templates. I want to include tags, which are then replaced with specific data from the controller.

I found a engine called razorengine that was exectly what I was looking for, but it is not compatible with .netcoreapp1.1. Are there other alternatives that I could use?

I am using Mailkit btw.

UPDATE:

I found a alternative called DotLiquid, Thanks anyways!

Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34
Dave
  • 290
  • 2
  • 12

2 Answers2

0

You may also use RazorLight which allows the same using the built-in Razor engine and runs with .Net Core.

Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34
0

You don't need to include an extra library for that.

Just create an html file that lies in your project scope and mark your placeholders e.g. via {{ ... }}

For example create a file wwwroot/emails/welcome.html:

Dear {{user}},

<p>Have fun with this page.</p>
<p>Please click <a href="{{link}}">here</a> to activate your account.</p>

Best wishes

Now in your controller action:

public async Task<IActionResult> SendSomeEmailTo(userId)
{
  string body;
  using (var file = System.IO.File.OpenText("wwwroot/emails/welcome.html"))  
  {
    body = file.ReadToEnd();
  }

  var user = this.userManager.findById(userId);
  // replace your placeholders here
  body = body
    .Replace("{{user}}", user.getFullName())
    .Replace("{{link}}", "www.foo.bar");

  // use the email body in your email service of your choice
  await sender.SendEmailAsync(user.Email, "Welcome", body);
}
Felix K.
  • 14,171
  • 9
  • 58
  • 72