java-How to send mail by using mailgun http api in java ?

In this article , I would demo how to send email via mailgun’s REST API in java applications. I would use the apache http client to do this job.


  • jdk1.8.0_40
  • SpringBoot 1.2.5

Maven dependencies


Step #0: Before you begin

You should get your mailgun’s domain and your api key in, just like this( Assuming that your domain name is

private static final String HTTP_API_KEY = "key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
private static final String HTTP_URL = "";

You should change the HTTP_URL with your own domain name.

Step #1: Setup http basic authentication

According to mailgun’s http api documentation, we should add http authentication headers in the http post request, we do like this:

We create a method to build apache httpclient’s HttpClient object like this:

private HttpClient buildHttpClient() {
    if(client!=null) return client;
    client = HttpClientBuilder.create().setDefaultCredentialsProvider(getAuthProvider()).build();
    return client;

Then we create a getAuthProvider to build the CredentialsProvider:

private CredentialsProvider getAuthProvider() {
    if(provider!=null) {
        return provider;
    provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials
            = new UsernamePasswordCredentials("api", HTTP_API_KEY);
    provider.setCredentials(AuthScope.ANY, credentials);
    return provider;

Step #2: Build the post form with fields

According to mailgun’s documentation, we should add fields to the http post form, so we create a method to do this job:

public void sendMail(String subject,String body,String recipients) {
    try {
        List<NameValuePair> form = new ArrayList<>();
        form.add(new BasicNameValuePair("from", "Example User<[email protected]>"));
        form.add(new BasicNameValuePair("to", recipients));
        form.add(new BasicNameValuePair("subject", subject));
        form.add(new BasicNameValuePair("text", body));
    catch (Exception e) {

Then we do the http post job with apache http client:

private String postMessage(String URL,List<NameValuePair> form) throws Exception {
    if(form==null||URL==null||URL.trim().length()==0) return "";
    StringBuilder stringBuilder = new StringBuilder();

    org.apache.http.client.HttpClient client = buildHttpClient();
    HttpPost httpPost = new HttpPost(URL);
    HttpResponse response = null;
    try {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);
        httpPost.setHeader("Accept", "application/json");
        response = client.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
  "post mailgun messages ok:"+URL);
        } else {
            log.warn("Failed to post mailgun,status=" + statusCode);
            throw new RuntimeException("post failed:"+URL);
    } catch (Exception e) {
        log.error( e.getMessage(), e);
        throw e;
    }finally {
        if(response!=null&&response.getEntity()!=null) {
    return stringBuilder.toString();

The getBody method is as follows:

private String getBody(InputStream inputStream) {
    String result = "";
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            result += inputLine;
            result += "\n";
    } catch (IOException ioe) {
    return result;

Step #3: Test

Then we can test the methods with this :

public static void main(String[] args) {
    try {
        EmailHttp http = new EmailHttp();
        for(int i=0;i<1;i++) {
            http.sendMail("This is a test mail","Hello world from mailgun, by using httpclient of apache","[email protected]");

    } catch (Exception e) {

If you get this message, you win:

16:47:07.152 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "  "id": "<[email protected]>",[\n]"
16:47:07.152 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "  "message": "Queued. Thank you."[\n]"
16:47:07.152 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "}"