Измените %y
на %Y
в строке ниже.
lastHourMessages = messages.Restrict("[ReceivedTime] >= '" + lastHourDateTime.strftime('%m/%d/%y %H:%M %p') + "'")
Просто чтобы быть уверенным, правильным является
lastHourMessages = messages.Restrict("[ReceivedTime] >= '" + lastHourDateTime.strftime('%m/%d/%Y %H:%M %p') + "'")
ОБНОВЛЕНИЕ # 1
Я добавил отладочную информацию в ваш код, чтобы понять, что происходит не так.
Попробуйте использовать приведенный ниже код. Надеюсь, это поможет вам.
import win32com.client
import datetime as dt
one_hour_before = dt.datetime.now() - dt.timedelta(minutes=60)
print(f'one_hour_before: {one_hour_before.astimezone()}')
# Get Outlook NameSpace object.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Get The Inbox folder.
# Other folder types:
# https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
inbox_folder = outlook.GetDefaultFolder(6)
print(f'{inbox_folder}\n')
# Get collection of Outlook items and sort.
messages = inbox_folder.Items
messages.Sort("[ReceivedTime]", True)
# Check all items and ReceivedTime.
for it, message in enumerate(messages):
print(f'{it}\t{message.ReceivedTime} - {message.Subject}')
# If recevied one hour before, print it.
received_time = dt.datetime.utcfromtimestamp(message.ReceivedTime.timestamp())
if received_time >= one_hour_before:
print(f'^^^\tRECEIVED ONE HOUR BEFORE')
# Get items received one hour before, if there are.
one_hour_before_messages = \
messages.Restrict( "[ReceivedTime] >= '"
+ one_hour_before.strftime('%m/%d/%Y %H:%M %p')
+ "'")
# Print them
if len(one_hour_before_messages) == 0:
print('No items received one hour before.')
else:
print('Items received one hour before:')
for one_hour_before_message in one_hour_before_messages:
print(f'{one_hour_before_message.ReceivedTime} \
- {one_hour_before_message.Subject}')
Результат кода выше:
one_hour_before: 2020-03-09 12:31:06.515809+09:00
受信トレイ
0 2020-03-09 13:19:04.102000+00:00 - RE: TEST
^^^ RECEIVED ONE HOUR BEFORE
1 2020-03-09 09:46:10.128000+00:00 - TEST
Items received one hour before:
2020-03-09 13:19:04.102000+00:00 - RE: TEST