Winter
Winter

Reputation: 87

How download attachments from secondary outlook email by Python?

I need download attachment from outlook, but not from my outlook.

I need it from secondary group address (like [email protected] with pass = asdf).

Right now I have working script that downloading it from my own outlook address.

    import os


path = os.path.expanduser("D:\DownloadingEmail\\replenishment")
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items


def saveattachemnts(subject):
    for message in messages:
        if message.Subject.startswith(subject):
            # body_content = message.body
            attachments = message.Attachments
            attachment = attachments.Item(1)
            for attachment in message.Attachments:
                attachment.SaveAsFile(os.path.join(path, str(attachment)))
                if message.Subject == subject and message.Unread:
                    message.Unread = False
                continue

saveattachemnts('Replenishment')

How can I modify it to download the attachment from inbox in [email protected]?

Upvotes: 1

Views: 682

Answers (2)

0m3r
0m3r

Reputation: 12499

To access the shared inbox try the following

inbox = outlook.Folders["[email protected]"].Folders["Inbox"]

also you should fix ("D:\DownloadingEmail\\replenishment") to ("D:\\DownloadingEmail\\replenishment")


SaveAsFile(os.path.join(path, str(attachment) should be SaveAsFile(os.path.join(path, str(attachment.FileName)


message.Unread = False to message.UnRead


see my example code below-

import os
import win32com.client
path = os.path.expanduser("D:\\DownloadingEmail\\replenishment")
print(path)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["[email protected]"].Folders["Inbox"]
messages = inbox.Items


def save_attachments(subject):
    for message in messages:
        if message.Subject.startswith(subject):

            for attachment in message.Attachments:
                attachment.SaveAsFile(os.path.join(path, str(attachment.FileName)))
                if message.UnRead:
                    message.UnRead = False
                continue


save_attachments('Replenishment')

Upvotes: 2

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Call outlook.CreateRecipient("[email protected]"), then pass the returned Recipient object to outlook.GetSharedDefaultFolder()

Upvotes: 0

Related Questions