MailerSendNetCore 0.0.7
dotnet add package MailerSendNetCore --version 0.0.7
NuGet\Install-Package MailerSendNetCore -Version 0.0.7
<PackageReference Include="MailerSendNetCore" Version="0.0.7" />
paket add MailerSendNetCore --version 0.0.7
#r "nuget: MailerSendNetCore, 0.0.7"
// 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 | Versions 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. |
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.Http.Polly (>= 7.0.11)
- Microsoft.Extensions.Options (>= 7.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 7.2.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.