← 返回首页
Usage Samples: Add Usage Documentation for Email Attachment Download by DuncBegg · Pull Request #110 · microsoftgraph/msgraph-sdk-python · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Usage Samples: Add Usage Documentation for Email Attachment Download#110

Open
DuncBegg wants to merge 2 commits into
microsoftgraph:mainfrom
DuncBegg:main
Open

Usage Samples: Add Usage Documentation for Email Attachment Download#110
DuncBegg wants to merge 2 commits into
microsoftgraph:mainfrom
DuncBegg:main

Conversation

Copy link
Copy Markdown

Pls provide a sample showing how to download a file attachment in an email.

eg Say i wanted to download an excel file from my mailbox.

Copy link
Copy Markdown
Contributor

This pull request has conflicting changes, the author must resolve the conflicts before this pull request can be merged.

Renji-FR commented May 2, 2023

Copy link
Copy Markdown

See #92 (comment)

Example:

request_message = self._msgraph_request.messages.by_message_id(message_id) request_attachment = request_message .attachments.by_attachment_id(attachment_id) query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters() request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration( query_parameters=query_params ) attachment = await request_attachment .get( request_configuration=request_config ) content = attachment.content_bytes content = base64.b64decode(content) content = content.decode('ascii')

CommunityHam commented May 25, 2023
edited
Loading

Copy link
Copy Markdown

See #92 (comment)

Example:

request_message = self._msgraph_request.messages.by_message_id(message_id) request_attachment = request_message .attachments.by_attachment_id(attachment_id) query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters() request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration( query_parameters=query_params ) attachment = await request_attachment .get( request_configuration=request_config ) content = attachment.content_bytes content = base64.b64decode(content) content = content.decode('ascii')

How are you getting the value for "attachment_ID" in this scenario? I cannot find any way to pull any information about the attachment other than "hasattachment", which is more a property of a message than an attachment. When I use the ".attachments" property on a mailitem, say, like, "message.attachments.ID", for example, it comes up Nonetype. I know I've authenticated properly, because I am still able to pull data from the body of the email, but cannot pull the attachment.

Copy link
Copy Markdown

@CommunityHam Hi, I use that:

async def _list_attachments(self, message:Message) -> None|list[Attachment]: query_params = AttachmentsRequestBuilder.AttachmentsRequestBuilderGetQueryParameters( ) request_config = AttachmentsRequestBuilder.AttachmentsRequestBuilderGetRequestConfiguration( query_parameters=query_params ) request_message = self._msgraph_request.messages.by_message_id(message.id) attachments:AttachmentCollectionResponse = await request_message.attachments.get( request_configuration=request_config ) if attachments is not None and attachments.value is not None: if attachments.odata_next_link is not None: pass return attachments.value else: return None

Then use the return value like this:

attachments = await self._list_attachments(message) if attachments is None: return None query_params = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetQueryParameters( ) request_config = AttachmentItemRequestBuilder.AttachmentItemRequestBuilderGetRequestConfiguration( query_parameters=query_params ) request_message = self._msgraph_request.messages.by_message_id(message.id) for attachment in attachments: request_attachment = request_message.attachments.by_attachment_id(attachment.id) ...

jussihi commented Sep 17, 2023

Copy link
Copy Markdown

Can't you get all the attachments to a single list with single call:

attachments = (await (graph_client.users.by_user_id(user_id).messages.by_message_id(message.id).attachments.get())).value

then you can check if entry is an ItemAttachment and get its content_bytes

baywet requested a review from a team as a code owner June 19, 2024 16:41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Footer

© 2026 GitHub, Inc.