Clearly documented reading of emails functionality with python win32com outlook

前端 未结 5 845
梦谈多话
梦谈多话 2020-12-12 09:27

I\'m trying to understand outlook interaction through win32com better. I\'ve been unable to find clear documentation that allows me to utilise win32com to read emails effec

相关标签:
5条回答
  • 2020-12-12 09:50

    The visual basic for applications reference is your friend here. Try starting with this link...

    Interop Outlook Mailitem Properties

    For instance I can see that message will probably have additional properties than what you listed above. For example.

    • message.CC
    • message.Importance
    • message.LastModificationTime
    0 讨论(0)
  • 2020-12-12 10:00

    For everyone wondering how to reach any default folder not just "Inbox" here's the list:

    3  Deleted Items
    4  Outbox
    5  Sent Items
    6  Inbox
    9  Calendar
    10 Contacts
    11 Journal
    12 Notes
    13 Tasks
    14 Drafts
    

    There are more (Reminders, Sync errors etc.); you can get whole list with this code (inspired by John Cook's solution to Folders):

    import win32com
    outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    for i in range(50):
        try:
            box = outlook.GetDefaultFolder(i)
            name = box.Name
            print(i, name)
        except:
            pass
    

    I'm not pasting the whole list here, because mine is in Polish and wouldn't be really helpful.

    0 讨论(0)
  • 2020-12-12 10:02

    I thought I'd add something on navigating through folders too - this is all derived from the Microsoft documentation above, but might be helpful to have here, particularly if you're trying to go anywhere in the Outlook folder structure except the inbox.

    You can navigate through the folders collection using folders - note in this case, there's no GetDefaultFolder after the GetNamespace (otherwise you'll likely end up with the inbox).

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace('MAPI')
    folder = outlook.Folders[1]
    

    The number is the index of the folder you want to access. To find out how many sub-folders are in there:

    folder.Count
    

    If there more sub-folders you can use another Folders to go deeper:

    folder.Folders[2]
    

    Folders returns a list of sub-folders, so to get the names of all the folders in the current directory, you can use a quick loop.

    for i in range(folder.Count):
        print (folder[i].Name)
    

    Each of the sub-folders has a .Items method to get a list of the emails.

    0 讨论(0)
  • 2020-12-12 10:10

    You can see all live Outlook objects and their data in OutlookSpy.

    MailItem object properties, methods and events are fully documented at https://msdn.microsoft.com/en-us/library/office/ff861332.aspx

    0 讨论(0)
  • 2020-12-12 10:11

    For attachments https://docs.microsoft.com/en-us/office/vba/api/outlook.attachment (see Properities)

    attachment.FileName
    attachment.Type
    attachment.Position
    attachment.BlockLevel
    attachment.Class
    attachment.DisplayName
    attachment.Parent
    attachment.Session
    attachment.Size
    attachment.Index
    attachment.Application
    
    0 讨论(0)
提交回复
热议问题