Skip to content

This project is an unofficial client for MailerSend and does not claim to be complete, I just added the use cases that I needed for my uses.

License

Notifications You must be signed in to change notification settings

yanezricardo/MailerSendNetCore

Repository files navigation

MailerSend SDK for .NET

This project provides an easy way to interact with the MailerSend API using C# and the .NET Framework. It is built on .NET 7 and uses Newtonsoft.Json for JSON serialization and deserialization.

This is an unofficial SDK for MailerSend and does not claim to be complete.

Getting Started

To start using this SDK, you will need to install it via NuGet or cloning and adding a reference in your project.

Installation

Install-Package MailerSendNetCore

Usage

Add "MailerSend" section to appsettings.json

  "MailerSend": {
    "ApiUrl": "https://api.mailersend.com/v1",
    "ApiToken": "<your MailerSend api token>",
    "UseRetryPolicy": true,
    "RetryCount": 5
  },

Configure the client using one of the following methods:

//METHOD #1: Read options from configuration (RECOMMENDED)
builder.Services.AddMailerSendEmailClient(builder.Configuration.GetSection("MailerSend"));

//METHOD #2: Set options from configuration manually
builder.Services.AddMailerSendEmailClient(options =>
{
    options.ApiUrl = builder.Configuration["MailerSend:ApiUrl"];
    options.ApiToken = builder.Configuration["MailerSend:ApiToken"];
});

//METHOD #3: Add custom options instance
builder.Services.AddMailerSendEmailClient(new MailerSendEmailClientOptions
{
    ApiUrl = builder.Configuration["MailerSend:ApiUrl"],
    ApiToken = builder.Configuration["MailerSend:ApiToken"]
});

Inject the client into your service, controller or handler

private readonly IMailerSendEmailClient _mailerSendEmailClient;

public EmailService(IMailerSendEmailClient mailerSendEmailClient)
{
    _mailerSendEmailClient = mailerSendEmailClient;
}

Send emailS

public async Task<string> SendEmail(string templateId, string senderName, string senderEmail, string[] to, string subject, MailerSendEmailAttachment[] attachments, IDictionary<string, string>? variables, CancellationToken cancellationToken = default)
{
    var parameters = new MailerSendEmailParameters();
    parameters
        .WithTemplateId(templateId)
        .WithFrom(senderEmail, senderName)
        .WithTo(to)
        .WithAttachment(attachments)
        .WithSubject(subject);

    if (variables is { Count: > 0 })
    {
        foreach (var recipient in to)
        {
            parameters.WithPersonalization(recipient, variables);
        }
    }

    var response = await _mailerSendEmailClient.SendEmailAsync(parameters, cancellationToken);
    if (response is { Errors.Count: > 0 })
    {
        //handle errors                
    }

    return response.MessageId;
}

Additional Resources

About

This project is an unofficial client for MailerSend and does not claim to be complete, I just added the use cases that I needed for my uses.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages