Sending Emails with Mailgun

The Mailgun API is built on HTTP and is RESTful. It uses predictable, resource- oriented URLs and built-in HTTP capabilities for passing parameters and authentication. The Mailgun API responds with standard HTTP response codes to indicate errors, and returns JSON.

Mailgun has published libraries for various languages. You can use these libraries or your favorite HTTP/REST library to make HTTP calls to Mailgun.

For more code samples in other programming languages, see Mailgun's documentation.

Pricing

As a Google Cloud Platform user, your first 30,000 messages are free every month. See the monthly pricing calculator on the sign up page for pricing on additional messages and volume discounts.

Setup

  • Add the required jersey REST client modules to your application. For example, if you use Maven, add to the project's pom.xml:

    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.19.3</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.19.3</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey.contribs</groupId>
      <artifactId>jersey-multipart</artifactId>
      <version>1.19.3</version>
    </dependency>

Examples

Sending a plain text message:

private ClientResponse sendSimpleMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
      + "/messages");
  MultivaluedMapImpl formData = new MultivaluedMapImpl();
  formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.add("to", recipient);
  formData.add("subject", "Simple Mailgun Example");
  formData.add("text", "Plaintext content");
  return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,
      formData);
}

Sending a message with HTML and text parts. This example also attaches files to the message:

private ClientResponse sendComplexMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
      + "/messages");
  FormDataMultiPart formData = new FormDataMultiPart();
  formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.field("to", recipient);
  formData.field("subject", "Complex Mailgun Example");
  formData.field("html", "<html>HTML <strong>content</strong></html>");
  ClassLoader classLoader = getClass().getClassLoader();
  File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
  formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
  return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
      .post(ClientResponse.class, formData);
}

Sample response:

{
    "message": "Queued. Thank you.",
    "id": "<20111114174239.25659.5817@samples.mailgun.org>"
}

Learning more

For more detailed examples and information, including how to track and route messages, see Mailgun's documentation.

Send feedback about...

App Engine flexible environment for Java docs