Documentation

Send E-mails

The Nomirun SDK includes built-in email sending capabilities with support for both SMTP servers and SendGrid. This allows you to integrate email delivery into your application without writing low-level plumbing code.

Below is an example of sending an email via an SMTP server. To get started, configure the email provider using environment variables or configuration files, then register the email sender service in your application.

Once registered, you can inject IEmailService<EmailMessage> into your class. This service is registered with a transient lifetime and provides a simple interface for composing and sending email messages.

Let me know if you’d like to include the code snippet as well or want an equivalent explanation for SendGrid!

For SMTP server:

IConfiguration configuration  = new ConfigurationBuilder()
                                    .AddJsonFile(...)
                                    .AddEnvironmentVariables(...);

services.ConfigureSMTPService(options =>
{
    options.FromName = configuration.GetValue<string>($"SmtpServer:FromName"),
    options.FromEmail = configuration.GetValue<string>($"SmtpServer:FromEmail"),
    options.Password = configuration.GetValue<string>($"SmtpServer:Password"),
    options.Username = configuration.GetValue<string>($"SmtpServer:Username"),
    options.Host = configuration.GetValue<string>($"SmtpServer:Host"),
    options.Port = configuration.GetValue<int>($"SmtpServer:Port"),
    options.UseSsl = configuration.GetValue<bool>($"SmtpServer:UseSsl")
});

or Sendgrid:

IConfiguration configuration  = new ConfigurationBuilder()
                                    .AddJsonFile(...)
                                    .AddEnvironmentVariables(...);
                                    
services.ConfigureSendGridService(options =>
{
    options.FromName = configuration.GetValue<string>($"SendGrid:FromName"),
    options.FromEmail = configuration.GetValue<string>($"SendGrid:FromEmail"),
    options.ApiKey = configuration.GetValue<string>($"SendGrid:ApiKey")
});

In your application code, you can construct an email message object and load the corresponding plain text and HTML templates. The built-in renderer processes these templates and sends the final composed message through the configured email service.

var newMessage = new NewMessage
{
    FromName = "John Doe",
    Message = "Welcome to the server.",
    SentOn = DateTime.UtcNow
};

var plainMessage = @"New message from {{ from_name }} at {{ sent_on }}:

{{message}}";

var htmlMessage = @"<html>
<body>
<p>New message from {{from_name}} at {{sent_on}}:</p>

<p>{{message}}</p>
</body>
</html>";


await _emailSender.Send(new List<string>() { "myemail@company.com" },
    $"New message from X",
    htmlMessage,
    plainMessage,
    newMessage);

Nomirun will replace {{ }} with values from the message object.