尝试获取发件人在 Outlook 中发送的最后一封电子邮件,以便我可以下载附件。
我遇到的问题是,电子邮件的发送日期并不是每天都一致发送,因此使用类似下面的内容是行不通的。
CurrentDateTime = dt.datetime.now() - dt.timedelta(days=1)
CurrentDateTime = CurrentDateTime.strftime('%d/%m/%Y %H:%M')
messages = messages.Restrict("[ReceivedTime] > '" + CurrentDateTime +"'")
下面的代码给了我正确的列表,但只想从最新的电子邮件中获取附件。
import win32com.client as client
ol_app = client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = ol_app.GetDefaultFolder(6)
messages = inbox.Items
for message in messages:
if message.SenderEmailAddress == '[email protected]':
print(message.Subject)
一旦我隔离了最后一封电子邮件,我计划使用“用于消息中的附件。附件:”。
您可以通过根据消息的接收时间按降序对消息进行排序,然后选择第一条消息(即最新的电子邮件)来实现此目的。
import win32com.client as client
ol_app = client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = ol_app.GetDefaultFolder(6)
messages = inbox.Items
messages.Sort("[ReceivedTime]", True) # Sort messages by ReceivedTime in descending order
latest_message = None
for message in messages:
if message.SenderEmailAddress == '[email protected]':
latest_message = message
break # Exit the loop after finding the latest message
if latest_message:
print(latest_message.Subject)
# Now you can iterate over the attachments in the latest message
for attachment in latest_message.Attachments:
# Download the attachment here
pass
else:
print("No email found from the specified sender.")