FlexiMail: Simplifying Email Integration for .NET Developers

Mabrouk Mahdhi
3 min readAug 13, 2024
Photo by Tiffany Tertipes on Unsplash

As developers, we often find ourselves dealing with complex email integrations, juggling multiple APIs, and managing configurations for different email systems. To address this challenge, I created FlexiMail, a powerful C# library designed to streamline email integration in .NET applications.

What is FlexiMail?

FlexiMail is a versatile library that supports both SMTP and Microsoft Exchange Web Services (EWS), allowing you to send emails effortlessly without diving into the intricacies of different protocols.

Key Features

  • EWS Support: Seamlessly integrates EWS.
  • Asynchronous Operations: Boost performance by handling email operations asynchronously.
  • Simple Configuration: Easily configure the library using straightforward options.
  • Testability: Designed with testability in mind, making it easy to write unit tests for your email-sending logic.

Why FlexiMail?

The inspiration behind FlexiMail was the need for a unified and easy-to-use solution that abstracts away the complexities of email integration. If you’re sending emails with the sophisticated features of Exchange, FlexiMail has you covered.

Getting Started

Installation

FlexiMail can be easily installed via Package Manager Console:

Install-Package FlexiMail

Or via .NET CLI:

dotnet add package FlexiMail

Basic Usage

Configuring and using FlexiMail is straightforward. Here’s a quick example to get you started:

using FlexiMail.Models.Configurations;
using FlexiMail.Models.Foundations.Bodies;
using FlexiMail.Models.Foundations.Messages;

namespace FlexiMail.Sample
{
class Program
{
static async Task Main(string[] args)
{
var configurations = new ExchangeConfigurations
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
TenantId = "your-tenant-id",
Authority = "https://login.microsoftonline.com/{your-tenant-id}",
Scopes = ["https://outlook.office365.com/.default"],
SmtpAddress = "your-sender-email"
};

var flexiMailClient = new FlexiMailClient(configurations);

var nessage = new FlexiMessage
{
To = ["email@domain.com"],
Cc = ["other-email@domain.com"],
Subject = "Keep testing FlexiMail",
Body = new FlexiBody
{
Content = "This is the message body. It can be a plain text or HTML.",
ContentType = BodyContentType.PlainText
}
};

await flexiMailClient.SendAndSaveCopyAsync(nessage);

Console.ReadKey();
}
}
}

Configuration

FlexiMail can be configured to use different email service providers. Configure the desired provider in your app’s configuration file:

{
"ExchangeConfigurations": {
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret",
"TenantId": "your-tenant-id",
"SmtpAddress": "your-smtp-address",
"Sid": "your-sid",
"PrincipalName": "your-principal-name",
"Authority": "your-authority",
"Scopes": [
"https://outlook.office365.com/.default"
]
}
}

Note: You can use SmtpAddress, Sid or PrincipalName in order to configure your exchange service.

FlexiMail can be configured to use Exchange configurations like this:

public static void AddFlexiMail(
this IServiceCollection services,
ExchangeConfigurations configurations)
{
services.AddSingleton(configurations);
services.AddTransient<IFlexiMailClient, FlexiMailClient>();
}

Simply provide your Exchange configurations in the app settings, and FlexiMail handles the rest.

Conclusion

With FlexiMail, you can focus on what truly matters — building great applications — without getting bogged down by email integration details. It’s open-source, easy to set up, and designed to work out of the box.

Check out the GitHub repository for more details and to get started today!

--

--

Mabrouk Mahdhi

Microsoft MVP, Principal Software Engineer @ B&O Service AG