Easily connect a .NET Core application with Mailjet
dotnet add package Unitee.MailjetApiClient.ApiClient
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
}
}
Also, the configuration can be defined as env, for example:
MailjetApi__ApiKeyPublic=xxxxxxx
...
builder.Services.AddMailjetApiClient(builder.Configuration);
Use the dependency injection to inject the service into your class.
private readonly IMailjetApiClient _iMailjetApiClient;
public FooService(IMailjetApiClient iMailjetApiClient)
{
_iMailjetApiClient = iMailjetApiClient;
}
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);
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);
You can use the GetContactId
to get id contact with mail.
var id = await _iMailjetApiClient.GetContactId("[email protected]");
You can use the DeleteContactFromContactList
to remove a contact from mailing list.
await _iMailjetApiClient.DeleteContactFromContactList("[email protected]", "idMailingList");
A dev container can be started at Docker/MailjetApiClient/ with docker-compose up -d for use with vscode remote.
Run dotnet test
.