Skip to content

Latest commit

 

History

History
93 lines (78 loc) · 1.93 KB

README.md

File metadata and controls

93 lines (78 loc) · 1.93 KB

MailerSend.AspNetCore NuGet

ASP.NET Core library for MailerSend

Installation

.NET CLI

dotnet add package MailerSend.AspNetCore

Package Manager

Install-Package MailerSend.AspNetCore

Configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMailerSend(options =>
        {
            options.ApiToken = "API-TOKEN";
            options.SenderEmail = "[email protected]";
            options.SenderName = "MailerSend";
        });
    }
}

appsettings.json

{
  "MailerSend": {
    "ApiToken": "API-TOKEN",
    "SenderEmail": "[email protected]",
    "SenderName": "MailerSend"
  }
}
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MailerSendOptions>(
            Configuration.GetSection("MailerSend"));
        services.AddMailerSend();
    }
}

Dependency injection

public class EmailController : ControllerBase
{
    private readonly MailerSendService _mailerSend;

    public EmailController(MailerSendService mailerSend)
    {
        _mailerSend = mailerSend;
    }

    [HttpPost("send")]
    public async Task<IActionResult> SendEmailAsync(CancellationToken ct)
    {
        var to = new List<Recipient>()
        {
            new Recipient()
            {
                Email = "[email protected]",
                Name = "User",
                Substitutions = new Dictionary<string, string>()
                {
                    { "var1", "value1"},
                    { "var2", "value2"}
                }
            }
        };

        await _mailerSend.SendMailAsync(
            to, subject: "subject", text: "Test text", cancellationToken: ct);

        return Ok();
    }
}