我正在尝试通过Microsoft Graph Sdk for Java发送带有附件的消息。
在首先创建草稿然后仅发布附件时,它可以工作,但是我无法使它直接与附件一起发布消息。
我在MS Graph SDK Java中使用Version 1.4.0
图形documentation明确指出,这应该可行:
在同一sendMail操作调用中,您可以:
- 包括附件
这是我到目前为止尝试过的:
({getClient()
返回一个有效的经过身份验证的IGraphServiceClient对象,我将其遗漏了此代码,因为它会增加过多的开销,并且没有为Question提供任何值-请参见GitHub page以获取有关示例的示例进行身份验证过程)
<!-- language: Java -->
// New message object
Message message = new Message();
// User email address object
EmailAddress senderEmailAddress = new EmailAddress();
senderEmailAddress.address = this.getProfile().getEmail();
senderEmailAddress.name = this.getProfile().getDisplayName();
// Set sender, from
Recipient from = new Recipient();
from.emailAddress = senderEmailAddress;
message.from = from;
message.sender = from;
message.toRecipients = new ArrayList<Recipient>();
EmailAddress ea = new EmailAddress();
ea.address = e;
Recipient recipient = new Recipient();
recipient.emailAddress = ea;
message.toRecipients.add(recipient);
// Set subject
message.subject = email.getSubject();
// Set body
ItemBody itemBody = new ItemBody();
itemBody.content = email.getBody();
itemBody.contentType = BodyType.HTML;
//Critical part
File a = new File("test.txt");
FileAttachment fileAttachment = new FileAttachment();
fileAttachment.name = a.getFilename();
byte[] src = FileUtils.readFileToByteArray(a);
//Encode base64
fileAttachment.contentBytes = ByteArraySerializer.serialize(src).getBytes();
fileAttachment.oDataType = "#microsoft.graph.fileAttachment";
fileAttachment.id = a.getName();
fileAttachment.size = src.length;
fileAttachment.contentType = "text/plain";
fileAttachment.isInline = false;
message.hasAttachments = true;
AttachmentCollectionResponse response = new AttachmentCollectionResponse();
response.value = Arrays.asList(fileAttachment);
message.attachments = new AttachmentCollectionPage(response, null);
getClient().me().sendMail(message, true).buildRequest().post();
我想这与我添加它的方式有关。 AttachmentCollectionResponsePage
似乎不是正确的方法。在here的C#示例中,我可以看到存在一个无参数构造函数,而在Java中则没有。
邮件已成功发送并存储到已发送邮件文件夹中,但没有附件。
任何帮助或想法将不胜感激!
更新: 同时,我为此找到了related GitHub issue。到目前为止,它已经在fixed的branch中,但尚未合并到当前版本1.5.0中。