【问题标题】:Python Outlook inbox saving attachments win32com.clientPython Outlook 收件箱保存附件 win32com.client
【发布时间】:2020-08-24 19:58:31
【问题描述】:

我已经获得了一些我想要的功能,但需要另外 2 个功能的帮助。

  1. 我想标记消息“标记为完成”(这是标记状态之一)。我还没有找到如何做到这一点。

  2. 如果我想为其他 4 封电子邮件做同样的事情,我将如何使用其他 4 个保存路径?

import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inboxfolder = Outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
inbox = inboxfolder.Items
message = inbox.GetFirst()
subject = message.Subject
sender = message.SenderEmailAddress

for m in inbox:
    if m.Class == 43: # this is to make sure it is an email item and not something else. 
        if m.SenderEmailAddress == 'John@email.com' and m.Unread == True:
            path = 'C:\\User\\Path\\Data\\John'
            print ('Subject as: ' and message)
            for attached in message.Attachments:
                attached.SaveASFile(os.path.join(path,attached.FileName)) #Saves attachment to current folder
                print (attached)
            message.Unread = False
            print (message.FlagStatus)
            message.FlagStatus = 1  # This is to "mark as Done" but it doesn't work
            message = inbox.GetNext()
        elif m.SenderEmailAddress == 'Jane@email.com' and m.Unread == True:
            path = 'C:\\User\\Path\\Data\\Jane'

            # ... How would you add 4 more? 
            message = inbox.GetNext()
        else:
            message = inbox.GetNext()

【问题讨论】:

    标签: python outlook win32com


    【解决方案1】:

    你必须保存它message.Save(),示例

    import win32com.client
    Outlook = win32com.client.Dispatch("Outlook.Application")
    olNs = Outlook.GetNamespace("MAPI")
    Inbox = olNs.GetDefaultFolder(win32com.client.constants.olFolderInbox)
    
    for Item in Inbox.Items:
        if Item.Class == 43:
            Item.FlagStatus = 1
            Item.Save()
    

    对于多个电子邮件和路径,请使用 dictionary,示例

    emails_with_path = {
        "email@one.com": "path_one",
        "email@two.com": "path_two",
        "email@three.com": "path_three"
    }
    
    for m in inbox:
        if m.Class == 43:
            for email, path in emails_with_path.items():
                if m.SenderEmailAddress == email and m.UnRead:
                    print(email)
                    print(path)
    

    【讨论】:

      猜你喜欢
      • 2014-04-19
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多