【发布时间】:2021-09-22 16:44:50
【问题描述】:
我正在尝试根据主题和发件人电子邮件扫描我的 Outlook 收件箱,然后尝试在特定位置本地下载任何附件。
此代码当前永远运行,但未检测到具有所需发件人地址和主题的电子邮件。
import win32com.client
import re
# set up connection to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetFirst()
while True:
try:
current_sender = str(message.Sender).lower()
current_subject = str(message.Subject).lower()
if re.search('The Subject I am scanning for',current_subject) != None and re.search('the sender email address to scan for',current_sender) != None:
print(current_subject)
print(current_sender)
attachments = message.Attachments
attachment = attachments.Item(1)
attachment_name = str(attachment).lower()
attachment.SaveASFile("Y:"+"\\" +"STRATEGIES"+"\\" + attachment_name)
else:
pass
message = messages.GetNext()
except:
message = messages.GetNext()
exit
理想情况下,一旦完成特定电子邮件附件的下载,该电子邮件将被放置在我的存档文件夹中,该文件夹位于索引 39 中。
【问题讨论】:
标签: python email outlook email-attachments