我正在使用 Python 3。 我正在尝试按日期提取(列出/打印显示)Outlook 电子邮件。
我正在尝试循环..可能是 WHILE 或 IF 语句。
可以吗,一个是字符串,一个是日期。 请考虑我到目前为止所得到的:谢谢。
1. import win32com.client, datetime
2.
3. # Connect with MS Outlook - must be open.
4. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
5. # connect to Sent Items
6. sent = outlook.GetDefaultFolder(5).Items # "5" refers to the sent item of a folder
7.
8. # Get yesterdays date
9. y = (datetime.date.today () - datetime.timedelta (days=1))
10. # Get emails by selected date
11. if sent == y:
12. msg = sent.GetLast()
13. # get Subject line
14. sjl = msg.subject
14. # print it out
15. print (sjl)
最佳答案
我完成了代码。感谢您的帮助。
`import sys, win32com.client, datetime
# Connect with MS Outlook - must be open.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace
("MAPI")
# connect to Sent Items
s = outlook.GetDefaultFolder(5).Items # "5" refers to the sent item of a
folder
#s.Sort("s", true)
# Get yesterdays date for the purpose of getting emails from this date
d = (datetime.date.today() - datetime.timedelta (days=1)).strftime("%d-%m-%
y")
# get the email/s
msg = s.GetLast()
# Loop through emails
while msg:
# Get email date
date = msg.SentOn.strftime("%d-%m-%y")
# Get Subject Line of email
sjl = msg.Subject
# Set the critera for whats wanted
if d == date and msg.Subject.startswith("xx") or msg.Subject.startswith
("yy"):
print("Subject: " + sjl + " Date : ", date)
msg = s.GetPrevious() `
这行得通。但是,如果找不到符合约束条件的消息,则不会退出。我试过 break,它只找到一条消息而不是所有消息,我想知道是否以及如何做一个异常(exception)?或者,如果我尝试 else d != date 它也不会起作用(它不会找到任何东西)。 我看不到 For 循环可以使用带有 msg(string) 的日期来工作。 我不确定——这里是 biginner :) ??
关于python - 在 Python 中按特定日期列出 Outlook 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914621/