Skip to content

uniteeio/dotnet-core-mailjet-client

Repository files navigation

Mailjet client

Nuget

Easily connect a .NET Core application with Mailjet

Install

dotnet add package Unitee.MailjetApiClient.ApiClient

Configuration

Configure using appSettings.json

Define a MailjetApi section in appsettings.

{
    "MailjetApi": {
        // required
        "ApiKeyPublic": "xxxxxxxxxxxxxxxxxxx", // mailjet public key (https://app.mailjet.com/account/api_keys)
        "ApiKeyPrivate": "xxxxxxxxxxxxxxxxxx", // mailjet private key (https://app.mailjet.com/account/api_keys)
       
        // optional (if null, use sender defined in the template) 
        "SenderEmail": "[email protected]", // email address used to send mails
        "SenderName": "unitee.io", // displayed name
       
         // optional (used in development)
        "IsSendingMailAllowed": "false", // disallow sending mails
        "TestingRedirectionMail": "[email protected]" // redirect all mails
    }
}

Configure using env

Also, the configuration can be defined as env, for example:

MailjetApi__ApiKeyPublic=xxxxxxx
...

Add Extensions in Program.cs

builder.Services.AddMailjetApiClient(builder.Configuration);

How to use

Inject the service

Use the dependency injection to inject the service into your class.

private readonly IMailjetApiClient _iMailjetApiClient;

public FooService(IMailjetApiClient iMailjetApiClient)
{
    _iMailjetApiClient = iMailjetApiClient;
}

Features

Send an email

You can use the SendMail method by following the example below to send an email via a Mailjet Template.

ℹ️ Some parameters are optionals (attachementFiles, variables, Cc mails)

var mailjetMail = new MailjetMail
{
    // required properties
    Users = new List<User>() { new User { Email = "[email protected]" } },
    TemplateId = MailjetTemplateId,

    // optionnal properties
    Variables = new { hello = "world" },

    UsersInCc = new List<User>() { new User { Email = "[email protected]" } },
    UsersInBcc = new List<User>() { new User { Email = "[email protected]" } },
    AttachmentFiles = new List<MailAttachmentFile>() 
    {
         new MailAttachmentFile
         {
             Filename = filename,
             ContentType = contentType,
             Base64Content = base64Content,
         }
    }
};

await _iMailjetApiClient.SendMail(mailjetMail);

You can also use the shorthand methods:

var email = "[email protected]";
var templateId = 1337;
var variables = new { foo = "foo", bar = "bar" };

var usersInCc = new List<User>() { new User { Email = "[email protected]" } };
var usersInBcc = new List<User>() { new User { Email = "[email protected]" } };

await _iMailjetApiClient.SendMail(email, templateId);
await _iMailjetApiClient.SendMail(email, templatedId, variables);
await _iMailjetApiClient.SendMail(email, templatedId, variables, usersInCc);
await _iMailjetApiClient.SendMail(email, templatedId, variables, usersInCc, usersInBcc);

Create or update a contact

You can use the AddOrUpdateContact method by following the example below to add or update a contact in mailjet and mailings lists.

var mailjetContact = new MailjetContact()
{
    // Required properties
    ContactEmail = "[email protected]",

    // Optionnal properties
    ContactName = "John Doe",
    ContactListId = 1,
    IsExcluded = false,
    CustomProperties = new Dictionary<string, string>
    {
        { "customProperties1", "value1" },
        { "customProperties2", "value2" }
    };
};

await _iMailjetApiClient.AddContact(mailjetContact);

Get contact id by mail

You can use the GetContactId to get id contact with mail.

var id = await _iMailjetApiClient.GetContactId("[email protected]");

Remove contact from mailings list

You can use the DeleteContactFromContactList to remove a contact from mailing list.

await _iMailjetApiClient.DeleteContactFromContactList("[email protected]", "idMailingList");

Development

Environment

A dev container can be started at Docker/MailjetApiClient/ with docker-compose up -d for use with vscode remote.

Run dotnet test.