Skip to content

Instantly share code, notes, and snippets.

@chhabraromit
Created January 3, 2015 12:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhabraromit/0c0a16545dd31f471332 to your computer and use it in GitHub Desktop.
Save chhabraromit/0c0a16545dd31f471332 to your computer and use it in GitHub Desktop.
Mailgun Example with Jersey 2.x
/*I have tried this with Jersey 2.14 only but should work for Jersey 2.x*/
package com.mail;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
/**
*
* @author chhabra.romit@gmail.com
*/
public class SendMail {
public static void main(String[] args) {
ClientConfig clientConfigMail = new ClientConfig();
Client clientMail = ClientBuilder.newClient(clientConfigMail);
clientMail.register(HttpAuthenticationFeature.basic("api",<your-api-key>));
WebTarget targetMail = clientMail.target("https://api.mailgun.net/v2/"+<your-mailgun-subdomain>+"/messages");
Form formData = new Form();
formData.param("from", "Mailgun Sandbox <postmaster@"+<your-mailgun-subdomain>+">");
formData.param("to", <to-email-address>);
formData.param("subject", <subject>);
formData.param("text", <text>);
Response response = targetMail.request().post(Entity.entity(formData,MediaType.APPLICATION_FORM_URLENCODED_TYPE));
//If everything goes correct you should be able to see status 200 in response
System.out.println("Mail sent : " + response);
}
}
@cicoub13
Copy link

cicoub13 commented Dec 5, 2016

👍 Thanks

@psanders
Copy link

First time go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment