java - 使用 Graph API 版本 2.3.2 发送带有多个附件的邮件(大小 < 4 MB)
问题描述
我正在尝试使用 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;
}
我已经仔细检查了文件的大小。3MBPDF.pdf = 3.6 MB 20KBPDF.pdf = 20 KB
总大小不大于 4 MB,但仍返回以下错误
严重:可抛出的详细信息:com.microsoft.graph.http.GraphServiceException:错误代码:BadRequest 错误消息:支持的最大请求长度为 4MB。
POST https://graph.microsoft.com/v1.0/me/microsoft.graph.sendMail SdkVersion:graph-java/v2.3.2 授权:[PII_REDACTED] {"saveToSentItems":true,"message":{"body ":{"内容[...]
413请求实体太大 [...]
我从 msgraph-sdk-java repo 的测试用例中捕获的上述代码片段。
请帮助我发送大小小于 4 MB 的电子邮件。谢谢
解决方案
您的请求超出了 Graph 请求的最大 4MB 限制,因为:
- 第一个pdf:3.6MB
- 由于 base64 编码,第一个 PDF 的空间使用量增加:2.2MB
- 第二个pdf:20kB
- 第二个 PDF 的空间使用增加:7kB
- JSON 有效负载:可能只有几 kB
总数远远超过 4MB 的限制。您需要为该 PDF使用大型附件上传。
推荐阅读
- drupal - Drupal 7 中的自定义 404 基本页面有空白标题标签
- python - 在另一个类方法中存储一个类的实例
- javascript - ZeroMQ (ZMQ) 在客户端提供 VivaGraph 图
- kubernetes - 如何测量用户在 Kubernetes 上创建作业与开始在节点上运行该作业的时间之间的时间?
- perl - 如何使用 Beam::Wire 获取服务名称?
- sql - SQL JOIN ON(有两个条件)
- c# - 使用 Unity 的 MVC 控制器构造函数注入中的工作单元和存储库模式不对数据库进行任何更改
- perl - Perl 中的 OS 页面对齐分配
- azure - Docker 上的 Azure CosmosDB 模拟器不生成证书
- javascript - ReactJS 类组件使用 contenteditable 元素渲染 array.map