JAVA在邮件枪中发送多个文件附件

ds97pgxw  于 2022-10-23  发布在  Java
关注(0)|答案(4)|浏览(177)

我正在尝试使用Mailgan发送电子邮件,并在此电子邮件中附加两个或更多文件:

public static JsonNode sendComplexMessage() throws UnirestException {

        HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + YOUR_DOMAIN_NAME + "/messages")
                .basicAuth("api", API_KEY)
                .queryString("from", "Excited User <USER@YOURDOMAIN.COM>")
                .queryString("to", "alice@example.com")
                .queryString("cc", "bob@example.com")
                .queryString("bcc", "joe@example.com")
                .queryString("subject", "Hello")
                .queryString("text", "Testing out some Mailgun awesomeness!")
                .queryString("html", "<html>HTML version </html>")
                .field("attachment", new File("/temp/folder/test.txt"))
                .asJson();

        return request.getBody();

这个例子来自Mailgan Docs,但它只发送单个文件。我需要发送多封电子邮件。
如有任何帮助,我们不胜感激。

eulz3vhy

eulz3vhy1#

不是放置单个文件对象,而是放置一个文件的arrayList,它的工作方式如下:

.field("attachment", Arrays.asList(file1,file2))

您可以创建一个列表,循环遍历它,然后发送它

List<File> listFiles=new ArrayList<>();
// fill it

.field("attachment", listFiles)
velaa5lx

velaa5lx2#

您可以再次使用.field("attachment", new File("FILE_NAME"))发送另一个附件,如以下代码所示:

public static JsonNode sendComplexMessage() throws UnirestException {
    HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + YOUR_DOMAIN_NAME + "/messages")
                    .basicAuth("api", API_KEY)
                    .queryString("from", "Excited User <USER@YOURDOMAIN.COM>")
                    .queryString("to", "alice@example.com")
                    .queryString("cc", "bob@example.com")
                    .queryString("bcc", "joe@example.com")
                    .queryString("subject", "Hello")
                    .queryString("text", "Testing out some Mailgun awesomeness!")
                    .queryString("html", "<html>HTML version </html>")

                    // attaching test.txt and test2.txt files
                    .field("attachment", new File("/temp/folder/test.txt"))
                    .field("attachment", new File("/temp/folder/test2.txt"))

                    .asJson();
}
yftpprvb

yftpprvb3#

如果列表中有文件名,则可以执行以下操作:

List<String> attachmentNames = ...
for (String attachmentName : attachmentNames) {
     multipartBody.field("attachment", new File(attachmentName));
}
a64a0gku

a64a0gku4#

很晚了,但是没有文件的版本,只有输入流

public class MyEmailAttachment {
    byte[] content;
    String name;
}
String baseUrl = getKey(Configuration.MAILGUN_BASEURL);
        String domain = getKey(Configuration.MAILGUN_DOMAIN);
        String apiKey = getKey(Configuration.MAILGUN_API_KEY);
        String from = getKey(Configuration.MAILGUN_FROM_SENDER);

        String url = baseUrl + "/v3/" + domain + "/messages"; 

        MultipartBody body = Unirest.post(url)
                .basicAuth("api", apiKey)
                .field("from", from)
                .field("to", to) //it's a List<String>
                .field("cc", cc) //it's a List<String>
                .field("subject", subject)
                .field("html", message);

        for (MyEmailAttachment mya : attachments) {
            body.field("attachment", new ByteArrayInputStream(mya.content), mya.name);
        }

        HttpResponse<JsonNode> request = body.asJson();
        //do something with request

相关问题