How to attach multiple files when sending emails via Mailgun in Java?

Hey everyone, I’m stuck trying to send emails with multiple file attachments using Mailgun in Java. I’ve got the basic setup working for a single attachment, but I can’t figure out how to add more than one file. Here’s what I’ve got so far:

public static JsonNode sendEmailWithAttachments() throws UnirestException {
    HttpResponse<JsonNode> response = Unirest.post("https://api.mailgun.net/v3/mydomain.com/messages")
            .basicAuth("api", "my-api-key")
            .queryString("from", "[email protected]")
            .queryString("to", "[email protected]")
            .queryString("subject", "Test Email")
            .queryString("text", "This is a test email with attachments.")
            .field("attachment", new File("/path/to/file.txt"))
            .asJson();

    return response.getBody();
}

This works fine for one attachment, but how can I modify it to send multiple files? I’ve tried adding more .field() calls, but that didn’t work. Any ideas on how to tackle this? Thanks in advance for your help!

I’ve dealt with this exact issue before, and I can share what worked for me. The trick is to use multiple .field() calls, but with a slight modification. Instead of using ‘attachment’ for each field, you need to use ‘attachment[0]’, ‘attachment[1]’, and so on. Here’s how I adjusted the code:

HttpResponse<JsonNode> response = Unirest.post("https://api.mailgun.net/v3/mydomain.com/messages")
    .basicAuth("api", "my-api-key")
    .queryString("from", "[email protected]")
    .queryString("to", "[email protected]")
    .queryString("subject", "Test Email")
    .queryString("text", "This is a test email with multiple attachments.")
    .field("attachment[0]", new File("/path/to/file1.txt"))
    .field("attachment[1]", new File("/path/to/file2.pdf"))
    .field("attachment[2]", new File("/path/to/file3.jpg"))
    .asJson();

This approach allows you to add as many attachments as needed. Just remember to increment the index for each new attachment. It’s been reliable for me in production code. Hope this helps solve your problem!

I’ve encountered this issue as well. The solution lies in using the MultipartBody class from Unirest. Here’s how you can modify your code to handle multiple attachments:

MultipartBody request = Unirest.post("https://api.mailgun.net/v3/mydomain.com/messages")
    .basicAuth("api", "my-api-key")
    .field("from", "[email protected]")
    .field("to", "[email protected]")
    .field("subject", "Test Email")
    .field("text", "This is a test email with multiple attachments.");

File[] attachments = {new File("/path/to/file1.txt"), new File("/path/to/file2.pdf")};
for (File file : attachments) {
    request.field("attachment", file);
}

HttpResponse<JsonNode> response = request.asJson();

This approach allows you to add any number of attachments in a loop. It’s more flexible and easier to maintain than manually indexing each attachment.

hey there, i’ve run into this before. try using the .fields() method instead of .field(). it lets u add multiple attachments like this:

.fields("attachment", Arrays.asList(
    new File("/path/to/file1.txt"),
    new File("/path/to/file2.pdf")
))

this way u can attach as many files as u need. hope that helps!