问题
Here is how i am getting list of attachment objects attached to a message:
IAttachmentCollectionRequest attachmentsPage = graphClient
.users(emailServer.getEmailAddress())
.mailFolders("Inbox")
.messages(mail.id)
.attachments()
.buildRequest();
List<Attachment> attachmentsData = attachmentsPage.get().getCurrentPage();
List<AttachmentData> attachmentDataToStore = new java.util.ArrayList<AttachmentData>();
for(Attachment attachment : attachmentsData)
{
attachmentData.setInputStream(
new ByteArrayInputStream(
attachment.getRawObject()
.get("contentBytes")
.getAsString()
.getBytes()));
}
Now, I believe the conversion of content bytes to input stream is not happening properly and eventually data(image.png) is getting corrupted. Any suggestions?
回答1:
Use the FileAttachment
class to handle all of this for you. Unfortunately the SDK doesn't handle this in the most "clean" manner - you have to request the attachment a second time.
public static void saveAttachments(String accessToken) {
ensureGraphClient(accessToken);
final String mailId = "message-id";
// Get the list of attachments
List<Attachment> attachments = graphClient
.me()
.messages(mailId)
.attachments()
.buildRequest()
// Use select here to avoid getting the content in this first request
.select("id,contentType,size")
.get()
.getCurrentPage();
for(Attachment attachment : attachments) {
// Attachment is a base type - need to re-get the attachment as a fileattachment
if (attachment.oDataType.equals("#microsoft.graph.fileAttachment")) {
// Use the client to generate the request URL
String requestUrl = graphClient
.me()
.messages(mailId)
.attachments(attachment.id)
.buildRequest()
.getRequestUrl()
.toString();
// Build a new file attachment request
FileAttachmentRequestBuilder requestBuilder =
new FileAttachmentRequestBuilder(requestUrl, graphClient, null);
FileAttachment fileAttachment = requestBuilder.buildRequest().get();
// Get the content directly from the FileAttachment
byte[] content = fileAttachment.contentBytes;
try (FileOutputStream stream = new FileOutputStream("C:\\Source\\test.png")) {
stream.write(content);
} catch (IOException exception) {
// Handle it
}
}
}
}
回答2:
Your method of reading contentBytes from the rawObject JSON will work. You just need to use a Base64 decoder instead of String.getBytes()
because contentBytes is base64-encoded. This was a bit faster on my network than requesting the attachment again using FileAttachmentRequestBuilder.
IAttachmentCollectionRequest attachmentsPage = graphClient
.users(emailServer.getEmailAddress())
.mailFolders("Inbox")
.messages(mail.id)
.attachments()
.buildRequest();
List<Attachment> attachmentsData = attachmentsPage.get().getCurrentPage();
List<AttachmentData> attachmentDataToStore = new java.util.ArrayList<AttachmentData>();
for(Attachment attachment : attachmentsData)
{
attachmentData.setInputStream(
new ByteArrayInputStream(
Base64.getDecoder().decode(attachment.getRawObject()
.get("contentBytes")
.getAsString());
}
来源:https://stackoverflow.com/questions/60971537/how-to-retrieve-office-365-mail-file-like-image-text-file-etc-attachment-using