A MJML API client for Java.
You can use this library to communicate with the MJML REST Api and convert your pre-built MJML to a HTML email.
This library currently supports V1 of the MJML API
- Gradle: In your
build.gradle
add the following:
repositories {
mavenCentral()
}
dependencies {
compile 'io.camassia:mjml-client:1.0.0'
}
- Maven: in your
pom.xml
add the followingrepository
anddependency
nodes
<project>
<dependencies>
<dependency>
<groupId>io.camassia</groupId>
<artifactId>mjml4j-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
MJMLClient client = MJMLClient.newDefaultClient()
.withApplicationID("...")
.withApplicationKey("...");
You can get an ApplicationID
and APIKey
from https://mjml.io/api
- Build a
RenderRequest
and call the client
RenderRequest request = new RenderRequest("<mjml>...<mjml>");
RenderResponse response = client.render(request);
String mjml = response.getMJML();
- Handling API errors
RenderRequest request = new RenderRequest("<mjml>...<mjml>");
try {
RenderResponse response = client.render(request);
String mjml = response.getMJML();
} catch (MJMLClientErrorException c) {
// Do something
// For example
// log.warn("Render error: " + c.getMessage() + ", request-id: " + c.getRequestId());
} catch (MJMLServerErrorException s) {
// Do something
// For example
// log.warn("Render error: " + s.getMessage() + ", request-id: " + s.getRequestId());
} catch (MJMLException e) {
// Do something
// For example
// log.warn("Render error: " + s.getMessage());
}
This library currently only supports the conversion of a pre-built MJML document to HTML. MJML document building is a work in progress but for now you can use libraries such as Jdom2/Jsoup to build your MJML Document Object Model. You can then use this library to convert it to a HTML email.