Structr
  • Welcome
  • Utilities
    • Abstractions
      • Check
      • Ensure
      • Money
      • HierarchyId
      • Providers
        • SequentialGuidProvider
      • Extensions
        • DateTime
        • Dictionary
        • DirectoryInfo
        • Enumerable
        • Enum
        • Expression
        • Int
        • Long
        • MemberInfo
        • Object
        • Queryable
        • ServiceCollection
        • String
        • Type
      • Helpers
        • AsyncHelper
        • BindHelper
      • JsonConverters
        • DateOnly
        • TimeOnly
        • StringNumber
    • Collections
      • AutoMapper extensions
    • IO
      • FileHelper
      • MimeTypeHelper
      • PathHelper
      • SequentialFileName
    • Configuration
      • Providers
        • JSON-file
        • XML-file
        • In-Memory
        • Consul
      • Get settings
      • Set settings
      • Customization
    • Email
      • Razor
    • Navigation
      • Menu
      • Breadcrumbs
    • Security
  • Domain
    • Domain
      • Entities
      • Value objects
  • Data Access
    • Entity Framework Core
    • Entity Framework 6
  • Use Cases
    • Operations
      • Filtering
      • Decoration
    • Notices
    • Validation
    • Specifications
    • Stateflows
      • StateMachine
      • Configurations
  • Presentation
    • ASP.NET Core
      • Client
      • Http
      • JavaScript
      • Json
      • Mvc
      • Referrer
      • Rewrite
      • Routing
      • TagHelpers
      • Validation
Powered by GitBook
On this page
  • Installation
  • Setup

Was this helpful?

Edit on GitHub
  1. Utilities
  2. Email

Razor

PreviousEmailNextNavigation

Last updated 2 years ago

Was this helpful?

You can use Razor templates instead of default templates ("{{" and "}}") for email notification content.

Installation

Email.Razor package is available on .

dotnet add package Structr.Email.Razor

Setup

Setup services:

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.
class Recipient
{
    public string Name { get; set; }
}

// Define static template file
class RecipientEmailTemplateFileMessage : EmailTemplateMessage<Recipient>
{
    public override string TemplatePath => "RecipientTemplate.cshtml"; // Absolute path is "C:\Templates\RecipientTemplate.cshtml".

    public RecipientEmailTemplateFileMessage(string to, Recipient model) 
        : base(new EmailAddress(to), model)
    {}
}

// Create instance of Model class.
var model = new Recipient { Name = "Peter Parker" };

// Create message
var message = new RecipientEmailTemplateFileMessage("to@example.com", model);

// Send email notification generated via template from "C:\Templates\RecipientTemplate.cshtml".
await _emailSender.SendEmailAsync(message);
NuGet