【发布时间】:2020-11-03 13:56:51
【问题描述】:
我正在尝试使用 Graph API 2.3.2 版发送带有多个附件的邮件(总大小
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;
}
我已经仔细检查了文件的大小。 3MBPDF.pdf = 3.6 MB 20KBPDF.pdf = 20 KB
总大小不大于 4 MB,但仍返回以下错误
严重:可投掷的细节: com.microsoft.graph.http.GraphServiceException:错误代码:BadRequest 错误信息:支持的最大请求长度为 4MB。
发布https://graph.microsoft.com/v1.0/me/microsoft.graph.sendMail SdkVersion:graph-java/v2.3.2 授权:[PII_REDACTED] {"saveToSentItems":true,"message":{"body":{"conten[...]
413 : 请求实体太大 [...]
上面的代码sn-p是我从msgraph-sdk-java repo的测试用例中截取的。
请帮助我发送大小小于 4 MB 的电子邮件。 谢谢
【问题讨论】:
标签: java microsoft-graph-api microsoft-graph-sdks microsoft-graph-mail