FluentEmail - All in one email sender for .NET and .NET Core is a great library to send e-mails.
Currently I have a full .NET 4.7.2 Console Application that makes use of FluentEmail.
My desire with it was to store the e-mail template (Razor .cshtml view) in an external class library and be able to use this view\template in the Console Application.
FluentEmail GitHub page has a sample code that shows how to consume that:
.Subject("Hey cool name!")
.UsingTemplateFromEmbedded("Example.Project.Namespace.template-name.cshtml",
new { Name = "Bob" },
new { Name = "Bob" },
TypeFromYourEmbeddedAssembly.GetType().GetTypeInfo().Assembly);
Steps:
1 - Create a Razor Class Library project. More on that can be found here:
Create reusable UI using the Razor Class Library project in ASP.NET Core
It uses .NET Standard 2.0 as its Target framework.
2 - Add Microsoft.AspNetCore.Mvc.Core NuGet package to the class library.
This is to have Intellisense, etc in Visual Studio.
Install-Package Microsoft.AspNetCore.Mvc.Core -Version 2.1.3
3 - Add a Razor view called EmailTemplate.cshtml to your class library.
4 - Right click it in Solution Explorer and select Properties. In Build Action, select Embedded resource.
5 - Add the following code to your Razor View:
2 - Add Microsoft.AspNetCore.Mvc.Core NuGet package to the class library.
This is to have Intellisense, etc in Visual Studio.
Install-Package Microsoft.AspNetCore.Mvc.Core -Version 2.1.3
3 - Add a Razor view called EmailTemplate.cshtml to your class library.
4 - Right click it in Solution Explorer and select Properties. In Build Action, select Embedded resource.
5 - Add the following code to your Razor View:
@model dynamic
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Some Title</title>
<base href="/" />
@*<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<script src="_framework/blazor.webassembly.js"></script>*@
</head>
<body>
E-mail template
Hey @Model.Name,<br>
<font size="14">You are so @Model.Compliment.</font>
</body>
</html>
Note that I’m using dynamic in the view code. This is just for testing. You can have full blown Models and use them in the templates as well.
6 - In your Console Application add a reference to the Class Library created in step 1.
7 - In your FluentEmail code, do the following in UsingTemplateFromEmbedded method:
email.To(configuration["emailTo"])
.SetFrom(configuration["emailFrom"])
.Subject(configuration["emailSubject"])
.UsingTemplateFromEmbedded(
"YourCompany.YourRazorClassLibrary.EmailTemplate.cshtml",
new { Name = "Leniel Maccaferri", Compliment = "Cool" },
Assembly.Load("YourCompany.YourRazorClassLibrary"));
The first parameter is the full class library (DLL) name + dot + Razor view name with .cshtml extension. The second parameter is the anonymous type that will be passed as the Model to the view and the third parameter is the class library assembly itself.
You're good to go!
This gives you a lot of flexibility.
You can add images, customize the HTML, CSS, have Layout pages, etc.
Hope it helps.