services.AddEmail("from@example.com", options => {options.TemplateRootPath="C:\\Templates"; // Important to configure this option when using email templates.}) .AddSmtpClient(host:"127.0.0.1", port:25) .AddRazorTemplateRenderer();
Important: Be sure that you configured TemplateRootPath option. It's an absolute path to root directory with email notification templates.
Example of using static template file:
// Define Model class for template.classRecipient{publicstring Name { get; set; }}// Define static template fileclassRecipientEmailTemplateFileMessage:EmailTemplateMessage<Recipient>{ public override string TemplatePath => "RecipientTemplate.cshtml"; // Absolute path is "C:\Templates\RecipientTemplate.cshtml".
publicRecipientEmailTemplateFileMessage(string to,Recipient model) : base(newEmailAddress(to), model) {}}// Create instance of Model class.var model =newRecipient { Name ="Peter Parker" };// Create messagevar message =newRecipientEmailTemplateFileMessage("to@example.com", model);// Send email notification generated via template from "C:\Templates\RecipientTemplate.cshtml".await_emailSender.SendEmailAsync(message);