The following sample binds settings from the configuration located in the section "Email" using SimpleInjector.
The settings contain sender Email and name, SMTP settings, Debug settings, PostProcessor settings and CSS settings. Check the EmailSettings class for details.
container.RegisterEmail(options => Configuration.GetSection("Email").Bind(options));To add new emails, following steps need to be done:
- Add a new View (
.cshtml) in the directoryViews/Emails/. - Add a new ViewModel in the same namespace as the business logic that needs to send the mail.
- Add the
EmailViewattribute to the ViewModel. The constructor argument is path to the view, relative to theViewsdirectory without the.cshtmlextension. Example:[EmailView("Emails/Registration")]points toViews/Emails/Registration.cshtml. - Extend the
EmailControllerwith a new method to render the view file and return the contents. - To send the mail in the business logic use the Mediator-command
SendEmailand supply it with the view model. SendEmail renders the mail based on the view model and theEmailViewAttribute.
To check the visuals of the view file, use the Swagger API to access the methods of the EmailController.
The views that are used for the emails are configured on the EmailViewAttribute. To find the view, the Razor view engine looks in the folder Views by default. A view path of Emails/FancyEmail matches to Views/Emails/FancyEmail.cshtml.
If you want to place the Views in other folders, for example /Emails, you can simply configure this in the Razor options as follows:
services.Configure<RazorViewEngineOptions>(options => options.ViewLocationFormats.Add("/Emails/{0}" + RazorViewEngine.ViewExtension));Per default the SubjectKey is "Subject" and it is being tried to resolve from the corresponding resource file. If you want to use a different key, you can set it in the EmailViewAttribute like this: [EmailView("Emails/Registration", SubjectKey = "RegistrationSubject")]. If no value could be found it will fallback to the the SubjectKey.
To add new emails, following steps need to be done:
- Add a new Component (
.razor) in the directoryComponents/Emails/. - Add a new ComponentModel in the same namespace as the business logic that needs to send the mail.
- Implement the
IComponentModel<T>interface in the ComponentModel, whereTis the type of the Component. - Extend the
EmailControllerwith a new method to render the razor file and return the contents. - To send the mail in the business logic use the Mediator-command
SendEmailand supply it with the view model. SendEmail renders the mail based on the component model and theIComponentModel<T>.
To check the visuals of the view file, use the Swagger API to access the methods of the EmailController.
In contrast to the classic Razor Views, the Razor Components can be placed in any folder, even in a different assembly, as long as you can reference it in the IComponentModel<T>.
Per default the SubjectKey is "Subject" and it is being tried to resolve from the corresponding resource file. If you want to use a different key, you can optionally implement the IProvideEmailSubject interface like this string IProvideEmailSubject.SubjectKey => "RegistrationSubject";. If no value could be found it will fallback to the the SubjectKey.
By default, only attachments in file://-Uris are supported. To allow adding attachments from other sources (eg. AWS S3), implement IEmailAttachmentResolver and register it SimpleInjector with
container.Collections.Append<IEmailAttachmentResolver, YourResolver>()No special Headers are sent per default, however with the Headers parameter on SendEmail, MimeKit.Headers can be added to the email (overrides all Headers defined in EmailOptions.DefaultHeaders). With EmailOptions.DefaultHeaders default headers can be set for all emails. Predefined sets of headers can be found in EmailHeaders:
DiscourageAutoRepliesincludesPrecedence:list,AutoSubmitted:generated, andX-Auto-Response-Suppress:All. They discourage email servers to sent auto replies.
On the EmailOptions email content post processors can be defined on the RazorContentPostProcessor (.cshtml) and the BlazorContentPostProcessor (.razor) property. Per default they both call the CssInliner to inline all styles into the tags themselves.
Providing null will instruct the renderer to not use a post processor.
Configuring the EmailOptions like seen in the sample below, all blazor emails will be processed by Mjml.Net before being sent.
container.RegisterSingleton<MjmlRenderer>();
container.RegisterEmail(o =>
{
// Other configurations...
o.BlazorContentPostProcessor = async ctx =>
{
var result = await container.GetInstance<MjmlRenderer>().RenderAsync(ctx.Html);
return result.Html;
};
});