Send Mail With Multiple Attachments (Size < 4 MB) Using Graph API Version 2.3.2

女生的网名这么多〃 提交于 2021-02-11 13:01:40

问题


I'm trying to send mail with multiple attachments (Sum of size is < 4MB) using Graph API version 2.3.2. Please find the following code snippet for the same.

public static void main(String[] a){
    try {
        TestBase testBase = new TestBase();
        Message message = getMessage();
        message.hasAttachments = true;
        AttachmentCollectionResponse response = new AttachmentCollectionResponse();
        response.value = Arrays.asList(
                getFileAttachment("/home/user/Downloads/3MBPDF.pdf"),
                getFileAttachment("/home/user/Downloads/20KBPDF.pdf"));
        message.attachments = new AttachmentCollectionPage(response, null);
        testBase.graphClient.me().sendMail(message, true).buildRequest().post();
    }catch (Exception e){
        e.printStackTrace();
    }
}

private static Message getMessage() {
    Message message = new Message();
    java.util.List<String> emails = Arrays.asList("vivektest@gmail.com");
    java.util.List<Recipient> listReceipient = new ArrayList<>();
    for(String email : emails) {
        EmailAddress emailAddress = new EmailAddress();
        emailAddress.address = email;
        Recipient recipient = new Recipient();
        recipient.emailAddress = emailAddress;
        listReceipient.add(recipient);
    }
    message.body = getItemBody();
    message.toRecipients = listReceipient;
    message.subject = "Test Message";
    message.id = "1234";
    return message;
}

private static ItemBody getItemBody() {
    ItemBody itemBody = new ItemBody();
    itemBody.content = "<html><head></head><body> test body </body></html>";
    itemBody.contentType = BodyType.HTML;
    return itemBody;
}

private static FileAttachment getFileAttachment(String path) throws Exception{
    FileAttachment fileAttachment = new FileAttachment();
    File pdfFile = new File(path);
    InputStream fileStream = new FileInputStream(pdfFile);
    fileAttachment.name = pdfFile.getName();
    fileAttachment.contentBytes = getByteArray(fileStream);
    fileAttachment.oDataType = "#microsoft.graph.fileAttachment";
    fileAttachment.size = Math.toIntExact((pdfFile.length() / 1024) / 1024);
    System.out.println("FileSize::: "+fileAttachment.size);
    fileAttachment.id="521";
    return fileAttachment;
}

public static byte[] getByteArray(InputStream in) {
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = in.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

I've double check the size of the files. 3MBPDF.pdf = 3.6 MB 20KBPDF.pdf = 20 KB

Total size is not greater then 4 MB but still its returning following error

SEVERE: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: BadRequest Error message: The maximum request length supported is 4MB.

POST https://graph.microsoft.com/v1.0/me/microsoft.graph.sendMail SdkVersion : graph-java/v2.3.2 Authorization : [PII_REDACTED] {"saveToSentItems":true,"message":{"body":{"conten[...]

413 : Request Entity Too Large [...]

The above code snippet I've captured from the test case of msgraph-sdk-java repo.

Please help me to send email with size less then 4 MB. Thanks


回答1:


Your request is exceeding the maximum 4MB limit for Graph requests as:

  • 1st pdf :3.6MB
  • space usage increase for 1st PDF due to base64 encoding : 2.2MB
  • 2nd pdf: 20kB
  • space usage increase for 2nd PDF: 7kB
  • JSON payload: probably a few kBs

the total being well over the 4MB limit. You need to use large attachment uploads for that PDF.



来源:https://stackoverflow.com/questions/64664240/send-mail-with-multiple-attachments-size-4-mb-using-graph-api-version-2-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!