我正在尝试检索电子邮件>2020年1月12日,但获得CCD_ 1。我尝试将.replace(microseconds=0)
添加到str(message.ReceivedTime)
,但仍然出现此错误。
错误
Traceback (most recent call last):
File "C:/Users/SLID/PycharmProjects/PPC_COASplitter/PPC_Ack.py", line 178, in <module>
get_url = readEmail()
File "C:/Users/zSLID/PycharmProjects/PPC_COASplitter/PPC_Ack.py", line 144, in readEmail
if str(message.ReceivedTime) >= '2020-12-1 00:00:00' and 'P1 Cust ID 111111 File ID' in message.Subject:
File "C:Program Files (x86)Python37-32libsite-packageswin32comclientdynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
ValueError: microsecond must be in 0..999999
代码
def readEmail():
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item('SharedMailbox, PPC-Investigation')
inbox = folder.Folders.Item('Inbox')
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
for message in messages:
if str(message.ReceivedTime) >= '2020-12-1 00:00:00' and 'P1 Cust ID 111111 File ID' in message.Subject:
print("")
print('Received from: ',message.Sender)
print("To: ", message.To)
print("Subject: ", message.Subject)
print("Received: ", message.ReceivedTime)
print("Message: ", message.Body)
get_url = re.findall(r'(https?://[^s]+)', message.Body)[2].strip('>')
return get_url
首先,将DateTime值与字符串进行比较。将字符串转换为日期时间值。
其次,永远不要循环浏览文件夹中的所有项目,而是使用items.Restrict(返回一个受限制的Items
集合(:
messages = messages.Restrict("[Received] >= '1/12/2020 0:00am' ")