This repo features the Java wrapper for the Mailjet API.
Check out all the resources and all the Java code examples on the Offical Documentation.
Add the following in your pom.xml
<dependencies>
<dependency>
<groupId>com.mailjet</groupId>
<artifactId>mailjet-client</artifactId>
<version>4.0.5</version>
</dependency>
</dependencies>
Grab your API and Secret Keys here. You need them for authentication when using the Email API:
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
For the V4 namespace (SMS API) the authorization method is based on a Bearer token. To generate a new token, please go Mailjet's SMS Dashboard and click on 'Generate a token'. Additional information can be found here.
MailjetClient client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions("v4"));
Requests are easy to understand:
MailjetRequest request = MailjetRequest(resource, id, actionId)
The Mailjet API is spread among three distinct versions:
v3
- The Email APIv3.1
- Email Send API v3.1, which is the latest version of our Send APIv4
- SMS API
Since most Email API endpoints are located under v3
, it is set as the default one and does not need to be specified when making your request. For the others you need to specify the version using ClientOptions
. For example, if using Send API v3.1
:
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions("v3.1"));
For additional information refer to our API Reference.
MailjetRequest contacts;
MailjetResponse response;
contacts = new MailjetRequest(Contact.resource);
response = client.get(contacts);
MailjetRequest contacts;
MailjetResponse response;
contacts = new MailjetRequest(Contact.resource)
.filter(Contact.LIMIT, 2);
response = client.get(contacts);
MailjetRequest contacts;
MailjetResponse response;
contacts = new MailjetRequest(Contact.resource, id);
response = client.get(contacts);
MailjetRequest contacts;
MailjetResponse response;
sender = new MailjetRequest(Sender.resource)
.property(Sender.EMAIL, "dummy@mailjet.com");
response = client.post(sender);
MailjetClient client;
MailjetRequest email;
MailjetResponse response;
// Note how we set the version to v3.1 using ClientOptions
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions("v3.1"));
JSONObject message = new JSONObject();
message.put(Emailv31.Message.FROM, new JSONObject()
.put(Emailv31.Message.EMAIL, "pilot@mailjet.com")
.put(Emailv31.Message.NAME, "Mailjet Pilot")
)
.put(Emailv31.Message.SUBJECT, "Your email flight plan!")
.put(Emailv31.Message.TEXTPART, "Dear passenger, welcome to Mailjet! May the delivery force be with you!")
.put(Emailv31.Message.HTMLPART, "<h3>Dear passenger, welcome to Mailjet</h3><br/>May the delivery force be with you!")
.put(Emailv31.Message.TO, new JSONArray()
.put(new JSONObject()
.put(Emailv31.Message.EMAIL, "passenger@mailjet.com")));
email = new MailjetRequest(Emailv31.resource).property(Emailv31.MESSAGES, (new JSONArray()).put(message));
response = client.post(email);
You can also use the previous version of Mailjet's Send API (v3). You can find the documentation explaining the overall differences and code samples here.
MailjetRequest getSender = new MailjetRequest(Sender.resource)
.filter(Sender.EMAIL, "email adress");
MailjetResponse senders = client.get(getSender);
// Grab the first sender
JSONObject sender = senders.getData().getJSONObject(0);
// Change the name
MailjetRequest update = new MailjetRequest(Sender.resource, sender.getLong("ID"))
.setBody(sender.put(Sender.NAME, "new name"));
System.out.println(client.put(update));
The proxy can be set using the following system properties:
HTTP proxy
-Dhttp.proxyHost=
-Dhttp.proxyPort=
HTTPS proxy
-Dhttps.proxyHost=
-Dhttps.proxyPort=
If you communicate with other endpoints using java.net.HttpURLConnection and you don't need proxy for them:
-Dhttp.nonProxyHosts=<any host you don't want to be proxied>
MailjetClient client;
MailjetRequest request;
MailjetResponse response;
// Note how we set the version to v4 using ClientOptions and use already generated token
MailjetClient client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions("v4"));
request = new MailjetRequest(Send.resource)
.property(Send.From, "MJPilot")
.property(Send.To, "+33600000000")
.property(Send.Text, "Have a nice SMS flight with Mailjet!");
response = client.post(request);