How to attach multiple files when sending emails through Mailgun API in Java

I need help with sending emails that have more than one file attached using Mailgun’s API in Java. Right now I can only get one file to work but I want to include several attachments in the same email.

Here’s my current code that works for a single attachment:

public static JsonNode sendEmailWithAttachments() throws UnirestException {
    HttpResponse<JsonNode> response = Unirest.post("https://api.mailgun.net/v3/" + DOMAIN_NAME + "/messages")
            .basicAuth("api", SECRET_KEY)
            .queryString("from", "Admin <[email protected]>")
            .queryString("to", "[email protected]")
            .queryString("subject", "Your Documents")
            .queryString("text", "Please find your documents attached")
            .queryString("html", "<p>Your requested files are attached below</p>")
            .field("attachment", new File("/documents/report.pdf"))
            .asJson();

    return response.getBody();
}

This works fine for one file but I can’t figure out how to add more files to the same email. I tried calling the field method multiple times but that doesn’t seem to work properly. What’s the correct way to handle multiple file attachments with Mailgun?

Thanks for any suggestions.

Yeah, this is a super common issue with Mailgun’s API. You’re doing it right by calling field multiple times - just need to tweak the approach. Chain multiple field calls for each attachment, but use the same “attachment” parameter name for all of them:

public static JsonNode sendEmailWithAttachments() throws UnirestException {
    HttpResponse<JsonNode> response = Unirest.post("https://api.mailgun.net/v3/" + DOMAIN_NAME + "/messages")
            .basicAuth("api", SECRET_KEY)
            .queryString("from", "Admin <[email protected]>")
            .queryString("to", "[email protected]")
            .queryString("subject", "Your Documents")
            .queryString("text", "Please find your documents attached")
            .queryString("html", "<p>Your requested files are attached below</p>")
            .field("attachment", new File("/documents/report.pdf"))
            .field("attachment", new File("/documents/invoice.pdf"))
            .field("attachment", new File("/documents/summary.docx"))
            .asJson();

    return response.getBody();
}

I’ve used this in production and it works perfectly. Mailgun wants multiple fields with the same name, not different parameter names for each file.

Hit this exact problem last year building a document delivery system. Took me way too long to figure out that order matters with Unirest.

What fixed it for me: build the request step by step, then add attachments after you’ve got the basic email stuff set up. Here’s what works:

public static JsonNode sendEmailWithAttachments() throws UnirestException {
    HttpRequest request = Unirest.post("https://api.mailgun.net/v3/" + DOMAIN_NAME + "/messages")
            .basicAuth("api", SECRET_KEY)
            .queryString("from", "Admin <[email protected]>")
            .queryString("to", "[email protected]")
            .queryString("subject", "Your Documents")
            .queryString("text", "Please find your documents attached")
            .queryString("html", "<p>Your requested files are attached below</p>");
    
    // Add all attachments
    request.field("attachment", new File("/documents/report.pdf"))
           .field("attachment", new File("/documents/contract.pdf"))
           .field("attachment", new File("/documents/terms.pdf"));
    
    return request.asJson().getBody();
}

This handles 2-15 attachments per email no problem.

just loop through ur files and call field() for each one. works every time for me. something like this:

File[] files = {new File("/docs/file1.pdf"), new File("/docs/file2.docx")};
for(File f : files) {
    request.field("attachment", f);
}

dont exceed Mailgun’s 25MB limit though or it’ll fail silently.