问题描述
@H_404_13@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 == '[email protected]' 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 == '[email protected]' and m.Unread == True: path = 'C:\\User\\Path\\Data\\Jane' # ... How would you add 4 more? message = inBox.GetNext() else: message = inBox.GetNext()
解决方法
您必须保存它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 protected]": "path_one","[email protected]": "path_two","[email protected]": "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)