Coder Social home page Coder Social logo

yanezricardo / mailersendnetcore Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 2.0 42 KB

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: MIT License

C# 100.00%

mailersendnetcore's Introduction

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

mailersendnetcore's People

Contributors

kaimonos avatar yanezricardo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mailersendnetcore's Issues

Ricardo, could you please add 'cc'

Hi Ricardo
Great code for Mailersend.
Can I suggest to please also add 'cc' in addition to the 'bcc' that you've already coded for.

Thanks
Cres

License file?

Hi @yanezricardo,

Great work with the library!

Just to let you know, I saw on NuGet that the library is MIT licensed, but couldn't find a license file in the repo...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.