MailerSendNetCore 0.0.7

dotnet add package MailerSendNetCore --version 0.0.7                
NuGet\Install-Package MailerSendNetCore -Version 0.0.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MailerSendNetCore" Version="0.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MailerSendNetCore --version 0.0.7                
#r "nuget: MailerSendNetCore, 0.0.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MailerSendNetCore as a Cake Addin
#addin nuget:?package=MailerSendNetCore&version=0.0.7

// Install MailerSendNetCore as a Cake Tool
#tool nuget:?package=MailerSendNetCore&version=0.0.7                

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

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.7 554 10/21/2024
0.0.6 230 10/5/2024
0.0.5 5,168 9/27/2023
0.0.4 1,177 1/18/2023
0.0.3 1,419 7/26/2022
0.0.2 430 7/22/2022
0.0.1 1,191 4/16/2021

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.