尝试从 Outlook 中获取过去 3 天的电子邮件,它几乎可以工作,但它会跳过电子邮件,知道为什么吗? [里面的代码]
Trying to get emails from the past 3 days from outlook, it almost works, but it's skipping emails, any idea why? [code inside]
所以这是代码...
import win32com.client, datetime
from datetime import timedelta
def checkTime():
date_filter_unformated = datetime.date.today() - timedelta(days=3)
date_filter = date_filter_unformated.strftime("%m/%d/%y %I:%M:%S")
current_message = all_messages.GetPrevious()
message_time = current_message.ReceivedTime
df_list = list(date_filter)
mt_list = list(str(message_time))
df_month, mt_month = int(''.join([df_list[0],df_list[1]])), int(''.join([mt_list[0],mt_list[1]]))
df_day, mt_day = int(''.join([df_list[3],df_list[4]])), int(''.join([mt_list[3],mt_list[4]]))
df_year, mt_year = int(''.join([df_list[6],df_list[7]])), int(''.join([mt_list[6],mt_list[7]]))
if mt_year < df_year:
return "Old"
elif mt_year == df_year:
if mt_month < df_month:
return "Old"
elif mt_month == df_month:
if mt_day < df_day:
return "Old"
else:
CurrentMessage(current_message)
return "Pass"
elif mt_month > df_month:
CurrentMessage(current_message)
return "Pass"
def CurrentMessage(cm):
print cm.Sender, cm.ReceivedTime
#
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
# the inbox. Can change that number to reference
# any other folder
all_messages = inbox.Items
current_message = all_messages.GetLast()
while True:
if checkTime() == "Pass":
continue
if checkTime() == "Old":
break
所以它应该 return 过去 3 天内所有电子邮件的发件人姓名和接收日期,但它 return 是 26 日(今天)和然后是 23 日(3 天前)的所有电子邮件,中间有 none。我不知道是什么导致它这样做。有什么建议吗?
您假设 Items 集合中的邮件已排序。只有在您调用 Items.Sort(在您的情况下为 all_messages.Sort("[ReceivedTime]", true)
)之后,集合才会按特定顺序排序。
所以这是代码...
import win32com.client, datetime
from datetime import timedelta
def checkTime():
date_filter_unformated = datetime.date.today() - timedelta(days=3)
date_filter = date_filter_unformated.strftime("%m/%d/%y %I:%M:%S")
current_message = all_messages.GetPrevious()
message_time = current_message.ReceivedTime
df_list = list(date_filter)
mt_list = list(str(message_time))
df_month, mt_month = int(''.join([df_list[0],df_list[1]])), int(''.join([mt_list[0],mt_list[1]]))
df_day, mt_day = int(''.join([df_list[3],df_list[4]])), int(''.join([mt_list[3],mt_list[4]]))
df_year, mt_year = int(''.join([df_list[6],df_list[7]])), int(''.join([mt_list[6],mt_list[7]]))
if mt_year < df_year:
return "Old"
elif mt_year == df_year:
if mt_month < df_month:
return "Old"
elif mt_month == df_month:
if mt_day < df_day:
return "Old"
else:
CurrentMessage(current_message)
return "Pass"
elif mt_month > df_month:
CurrentMessage(current_message)
return "Pass"
def CurrentMessage(cm):
print cm.Sender, cm.ReceivedTime
#
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
# the inbox. Can change that number to reference
# any other folder
all_messages = inbox.Items
current_message = all_messages.GetLast()
while True:
if checkTime() == "Pass":
continue
if checkTime() == "Old":
break
所以它应该 return 过去 3 天内所有电子邮件的发件人姓名和接收日期,但它 return 是 26 日(今天)和然后是 23 日(3 天前)的所有电子邮件,中间有 none。我不知道是什么导致它这样做。有什么建议吗?
您假设 Items 集合中的邮件已排序。只有在您调用 Items.Sort(在您的情况下为 all_messages.Sort("[ReceivedTime]", true)
)之后,集合才会按特定顺序排序。