[英]How to get date & time of received outlook mail and check body from particular sender using python
I am trying to achieve the following using python: get the last 24hrs outlook inbox mails from the particular sender and checking if a particular text is presented in the body of that email我正在尝试使用 python 实现以下目标:从特定发件人获取最近 24 小时的 Outlook 收件箱邮件,并检查该电子邮件正文中是否显示了特定文本
below is I tried :下面是我试过的:
import sys, win32com.client, datetime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
s = outlook.GetDefaultFolder(6).Items
# 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")
# Set the critera for whats wanted
if (d == date) and ("MAINTENANCE" in msg.body) and ("Upgrade" in msg.Subject) and msg.Sender == "aa@bb.com" :
print("Date : ", date)
msg = s.GetPrevious()
error :错误 :
date = msg.SentOn.strftime("%d-%m-%y") File "C:\\Users\\1024983\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32com\\client\\dynamic.py", line 527, in getattr raise AttributeError("%s.%s" % (self. username , attr)) AttributeError: GetPrevious.SentOn
date = msg.SentOn.strftime("%d-%m-%y") 文件 "C:\\Users\\1024983\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32com\\client\\dynamic. PY”,线路527,在GETATTR加注AttributeError的( “%S%S” %(自用户名,ATTR)。)AttributeError的:GetPrevious.SentOn
and notsure what is the error in the checking line并不确定检查行中的错误是什么
if (d == date) and ("MAINTENANCE" in msg.body) and ("Upgrade" in msg.Subject) and msg.Sender == "aa@bb.com" :
Firstly, never loop through all item in a folder - you wouldn't use a SELECT statement in SQL without a WHERE clause, would you?首先,永远不要遍历文件夹中的所有项目——如果没有 WHERE 子句,你不会在 SQL 中使用 SELECT 语句,对吗? Always use
Items.Find/FindNext
or Items.Restrict
.始终使用
Items.Find/FindNext
或Items.Restrict
。
Secondly, Sender
is not a scalar (string) property - it returns an object ( AddressEntry
), so what you want is MailItem.Sender.Address
(after checking that MailItem.Sender
is no null).其次,
Sender
不是标量(字符串)属性 - 它返回一个对象( AddressEntry
),所以你想要的是MailItem.Sender.Address
(在检查MailItem.Sender
不为空之后)。
Thirdly, besides MailItem
objects in your Inbox folder, you can also have other items, such as MeetingItem
and ReportItem
whcih may not expose all the properties you are accessing (such as Sender
) .第三,除了收件箱文件夹中的
MailItem
对象之外,您还可以拥有其他项目,例如MeetingItem
和ReportItem
, ReportItem
可能不会公开您正在访问的所有属性(例如Sender
)。 If you only want MailItem
objects, check that the Class
property (exposed by all OOM objects) == 43 ( OlObjectClass.olMail
).如果您只需要
MailItem
对象,请检查Class
属性(由所有 OOM 对象公开)== 43 ( OlObjectClass.olMail
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.