Confinity Documentation
  • Latest Version
  • Latest Version
  • Getting Started

    • Introduction
    • Core Concepts
    • Create an Application
    • Glossary
  • Essentials

    • Authentication & SSO
    • Breaking Changes
    • Roslyn Source Analyzers
    • Changelog
    • ConfinityContent
    • ConfinitySelectable
    • Confinity Schedules
    • Data Seeding
    • Development guidelines [WIP]
    • Entity App
    • Entity Form
    • Entity Permissions
    • Frontend Configuration
    • Images
    • Known Issues
    • Localization
    • Migrations
    • Modules [WIP]
    • On-Site Editing
    • Settings
    • Cascade Delete
    • Replication
    • Infrastructure
  • Modules

    • Analytics Module
    • Assets Module
    • Blog Module
    • Cookie Consent Module
    • Forms Module
    • Friendly Captcha (Forms Module )
    • GeoIP Module
    • Htmx
    • Mail Module
    • Mailing Module
    • MediaPlayer Module
    • GoogleMyBusiness Module
    • OpenTelemetry Module
    • Pages Module [WIP]
    • Pattern Library Module
    • SIX Saferpay (worldline) Module
    • Products Module
    • Search Module
    • Wizard Module
  • Guides

    • Create a Custom Entity App Form Element
    • Date and Time
    • Entity Change Listener
    • File Upload / Temp File
    • HTTP security headers
    • conventions [WIP]
    • How to use Confinity with nginx
    • Notifications
    • Nullability
    • Rename Entity
    • Schedules
    • Useful snippets
    • Content Localization
  • Design Guidelines

    • Introduction
    • Page Components
    • Forms Module

Mail Module

Allows you to send mails. Contains a helper to render razor pages for a html body.

Configuration

In order for the Mail Module to work the mail server must be configured. The following default configuration expects a mail server running locally without authentication.

{
    "ConfinityMail" : {
        "Server": "localhost",
        "Port": 25,
        "Username": null,
        "Password": null,
        "EnableSsl": false,
        "FromEmailAddress": null
    }
}

Linux: In order for e-mail authentication to work, the package gss-ntlmssp must be installed.

Usage Send Mail

Inject IMailService an call the SendAsync method.

Simple example:


_mailService.SendAsync(message => message.To("root@localhost")
    .From("noreply@localhost")
    .Subject("hello world")
    .TextBody("this is the body \n bye"));

Example with razor view:

See Render Razor View for an explanations.


public class MailLaunchExample : IBackendActionSingle<ConfinityRowSelection, BackendActionSimpleResult>
{
    private readonly IMailService _mailService;
    private readonly IConfinityDbContext _dbContext;
    private readonly IRazorToStringRenderer _renderer;

    public MailLaunchExample(IMailService mailService, IConfinityDbContext dbContext,
        IRazorToStringRenderer renderer)
    {
        _mailService = mailService;
        _dbContext = dbContext;
        _renderer = renderer;
    }

    public async Task<BackendActionSimpleResult> Execute(ConfinityRowSelection selectedAsset)
    {
        var launch = _dbContext.Set<Launch>().Single(a => a.Id == selectedAsset.Id);

        string subject = "Launch " + launch.Name;
        string body = $"The launch is expected to be executed between {launch.WindowStart} and {launch.WindowEnd}";

        try
        {
            var htmlBody =
                await _renderer.RenderToStringAsync(MailModule.TitleTextMailView,
                    new TitleTextMailModel(subject, body));

            await _mailService.SendAsync(message => message.To("rim@conx.ch")
                .From("noreply@conx.ch")
                .Subject(subject)
                // .AlternateTextBody(body)
                .HtmlBody(htmlBody));
        }
        catch (Exception e)
        {
            return new FailedSimpleBackendActionResult("unable to send mail: " + e.Message);
        }

        return new SuccessfulSimpleBackendActionResult("mail sent");
    }
}

Render Razor view

To render a Razor view as body, you have to render it to a string. Our helper for this is IRazorViewToStringRenderer.

  1. In _ViewStart specify your layout:
@{ 
    Layout = "ConfinityMail/DefaultMailTemplate";
}

  1. Create your view
  2. Call the method RenderViewToStringAsync from IRazorViewToStringRenderer and pass in your view and your model.

Current Limitations

  • fire and forget - when the mail server is offline no resend is made.
Prev
Htmx
Next
Mailing Module