Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Graph API 2.3.2版发送包含多个附件(大小<;4 MB)的邮件_Java_Microsoft Graph Api_Microsoft Graph Sdks_Microsoft Graph Mail - Fatal编程技术网

Java 使用Graph API 2.3.2版发送包含多个附件(大小<;4 MB)的邮件

Java 使用Graph API 2.3.2版发送包含多个附件(大小<;4 MB)的邮件,java,microsoft-graph-api,microsoft-graph-sdks,microsoft-graph-mail,Java,Microsoft Graph Api,Microsoft Graph Sdks,Microsoft Graph Mail,我正在尝试使用Graph API 2.3.2版发送带有多个附件的邮件(大小总和

我正在尝试使用Graph API 2.3.2版发送带有多个附件的邮件(大小总和<4MB)。请查找以下代码段,以获得相同的结果

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;
}
publicstaticvoidmain(字符串[]a){
试一试{
TestBase TestBase=新的TestBase();
Message=getMessage();
message.hasAttachments=true;
AttachmentCollectionResponse=新的AttachmentCollectionResponse();
response.value=Arrays.asList(
getFileAttachment(“/home/user/Downloads/3MBPDF.pdf”),
getFileAttachment(“/home/user/Downloads/20KBPDF.pdf”);
message.attachments=新的AttachmentCollectionPage(响应,空);
testBase.graphClient.me().sendMail(message,true.buildRequest().post();
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态消息getMessage(){
消息消息=新消息();
java.util.List emails=Arrays.asList(“vivektest@gmail.com");
java.util.List ListReceipent=new ArrayList();
用于(字符串电子邮件:电子邮件){
EmailAddress EmailAddress=新的EmailAddress();
emailAddress.address=电子邮件;
收件人=新收件人();
recipient.emailAddress=电子邮件地址;
ListReceipent.add(收件人);
}
message.body=getItemBody();
message.toRecipients=ListRecipient;
message.subject=“测试消息”;
message.id=“1234”;
返回消息;
}
私有静态ItemBody getItemBody(){
ItemBody ItemBody=新的ItemBody();
itemBody.content=“测试主体”;
itemBody.contentType=BodyType.HTML;
返回项体;
}
私有静态FileAttachment getFileAttachment(字符串路径)引发异常{
FileAttachment FileAttachment=新文件附件();
文件pdfFile=新文件(路径);
InputStream fileStream=新文件InputStream(Pdfile);
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”;
返回文件附件;
}
公共静态字节[]getByteArray(InputStream in){
试一试{
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
国际nRead;
字节[]数据=新字节[16384];
while((nRead=in.read(data,0,data.length))!=-1){
写入(数据,0,nRead);
}
buffer.flush();
返回buffer.toByteArray();
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
我已经仔细检查了文件的大小。 3MBPDF.pdf=3.6MB 20KBPDF.pdf=20kb

总大小不大于4 MB,但仍返回以下错误

严重:可丢弃的细节: com.microsoft.graph.http.GraphServiceException:错误代码:BadRequest 错误消息:支持的最大请求长度为4MB

职位 SdkVersion:graph java/v2.3.2授权:[PII_已编辑] {“SaveToSetItems”:true,“message”:{“body”:{“conten[…]

413:请求实体太大[…]

上面的代码片段是我从msgraph sdk java repo的测试用例中捕获的

请帮我发送小于4MB的电子邮件。
谢谢

您的请求超出了图形请求的最大4MB限制,如下所示:

  • 第1版pdf:3.6MB
  • 由于base64编码,第一个PDF文件:2.2MB
  • 第二版pdf:20kB
  • 第二版PDF的空间使用量增加:7kB
  • JSON负载:可能只有几kBs

总数远远超过4MB限制。您需要使用该PDF。

您的请求超出了图形请求的最大4MB限制,如下所示:

  • 第1版pdf:3.6MB
  • 由于base64编码,第一个PDF文件:2.2MB
  • 第二版pdf:20kB
  • 第二版PDF的空间使用量增加:7kB
  • JSON负载:可能只有几kBs

总数远远超过4MB的限制。您需要使用该PDF。

感谢@baywet的快速回复。您能给我推荐一个示例,其中我可以附加5个1MB文件和一个6MB文件作为参考,这里创建了另一个问题,感谢@baywet的快速回复。您能给我推荐一个示例吗可以附加五个1MB文件和一个6MB文件以供参考,这里为这个问题创建了另一个问题