Im using Graph API to download only the excel file attachments in my outlook, ive used the correct end points and everything (i know this as the email information is correct and also the email attachment names are correct).

Aditya Dutt 0 Reputation points
2024-04-22T07:28:19.72+00:00

Python codeWhen i open the downloaded file.

Screenshot 2024-04-22 125132

endpoint = f'https://graph.microsoft.com/v1.0/users/{user_id}/messages'

# Fetch the read emails
response = requests.get(endpoint, headers=headers)

if response.status_code != 200:
    print(f"Failed to fetch emails: {response.text}")
    exit(1)

# Process the emails
emails = response.json().get('value', [])
for email in emails:
    email_id = email['id']
    email_has_attachments = email['hasAttachments']
    sub = email['subject']
    email_isRead = email['isRead']
    email_from = email['from']
    email_sender = email['sender']

    # Check if the email has attachments
    if email_has_attachments :
        print(f"Email from has {email_from['emailAddress']['name']}, email address {email_from['emailAddress']['address']} has attachments.")

        # Fetch the attachments
        attachments_endpoint = endpoint + f"/{email_id}/attachments"
        attachments_response = requests.get(attachments_endpoint, headers=headers)

        if attachments_response.status_code != 200:
            print(f"Failed to fetch attachments: {attachments_response.text}")
            continue

        attachments_response_json = attachments_response.json().get('value', [])
        for attachment in attachments_response_json:
            # Check if the attachment is an Excel file
            attachment_name = attachment['name']
            if attachment_name.endswith('.xlsx'):
                print(f"Downloading Excel attachment: {attachment_name}")
                attachment_id = attachment['id']
                download_attachment_endpoint = attachments_endpoint + f"/{attachment_id}" # /$value
                headers = {
                    "Authorization": f"Bearer {access_token}",
                    "ContentType": "application/octet-stream"
                }
                download_attachment_response = requests.get(attachments_endpoint, headers=headers)
                if download_attachment_response.status_code == 200:
                    print(f' Saving file : {attachment_name}')
                    with open(download_folder + f'/{attachment_name}', 'wb') as _f:
                        _f.write(download_attachment_response.content)

After running the above python code, the email attachment is saved on my device but when i open it in excel it shows this dialog box (above image).

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,507 questions
Outlook
Outlook
A family of Microsoft email and calendar products.
4,032 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,289 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,245 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,288 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.