Using Razor View Engine class and MailKit
MAilKit:
- Signup on site ethereal.email for fake SMTP service. Which allows to setup SMTP and a fake email process check.
- Add Nuget Package MAilKit 3.4.1 by Jeffrey Stedfast; Microsoft also suggest to use this one rather than System.Net
- Add respective generated email address and password in step 1 in AsppSettings.json or in code directly for testing purpose.
- Test ViewStringController -> EmployeeHtmlEmail()
- Generic Service component is available under Services -> EmailService.cs
- Check email on same etherel.email infterface under message
NOTE: Change new values from Etherel.email after signup
To Geenerate HTML string from/of a View:
- Add Interface IViewRendererService
- In ViewRendererService by using RazorViewEngine setup to write ViewEngineResult in stream writer which process as a string output.
- ViewEngine.FindView() Find the respective View in Controller, View Name and Model e.g. var emailHtmlFromView = await this._renderer.RenderAsync(this, "Index", GetEmployees());
- Write ViewContext in stream
- String can be further use as respective HTML rendered.
Reference:
IRazorViewEngine Interface (Microsoft.AspNetCore.Mvc.Razor) | Microsoft Learn
RazorViewEngine Class (Microsoft.AspNetCore.Mvc.Razor) | Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razor.razorviewengine?view=aspnetcore-6.0
RazorViewEngine.FindView(ActionContext, String, Boolean) Method (Microsoft.AspNetCore.Mvc.Razor) | Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razor.razorviewengine.findview?view=aspnetcore-6.0