This repo features the Brand new Java wrapper for the Mailjet API. Check out 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 keys here Alternatively for V4 namespace you will need a Bearer token. To generate a new token, please go Mailjet's SMS Dashboard and click on 'Generate a token'. Any additional information can be found here.
MailjetClient client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"));
For V4 namespace the authorization method is changed - now it is based on Bearer token.
MailjetClient client = new MailjetClient(System.getenv("MJ_TOKEN"), new ClientOptions("v4"));
Requests are easy to understand
MailjetRequest request = MailjetRequest(resource, id, actionId)
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);
MailjetRequest email;
JSONArray recipients;
MailjetResponse response;
recipients = new JSONArray()
.put(new JSONObject().put(Contact.EMAIL, "roger@mailjet.com"))
.put(new JSONObject().put(Contact.EMAIL, "stan@mailjet.com"))
.put(new JSONObject().put(Contact.EMAIL, "francine@mailjet.com"));
email = new MailjetRequest(Email.resource)
.property(Email.FROMNAME, "Guillaume")
.property(Email.FROMEMAIL, "dummy@email.fr")
.property(Email.SUBJECT, "Subject")
.property(Email.TEXTPART, "Java is coming!...")
.property(Email.RECIPIENTS, recipients)
.property(Email.MJCUSTOMID, "JAVA-Email");
response = client.post(email);
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);
- head