使用 Python 中的 "exchangelib" 在特定日期范围内(用户输入)从 Outlook 中的特定文件夹访问电子邮件
Access email from a particular folder in Outlook, within a specific date range (user Input) using "exchangelib" in Python
要求摘要:
要在用户给定日期范围内访问 Outlook 中特定文件夹中的电子邮件,
ex: all mails from June or all mails from 23-June-2020 to 15-July-2020
到目前为止,我们已经尝试了以下方法,但问题是:
- 日期范围没有给出正确的输出
- 输出时间太长,有时返回超时错误。
代码:
from exchangelib import Credentials, Account, DELEGATE, Configuration, IMPERSONATION, FaultTolerance,EWSDateTime,EWSTimeZone,Message
import datetime
import pandas as pd
new_password = "your password"
credentials = Credentials(username = 'username', password = new_password)
config = Configuration(server ='outlook.office365.com', credentials = credentials)
account = Account(primary_smtp_address ='username', credentials = credentials, autodiscover = False, config = config, access_type = DELEGATE)
#first approach.....................................
conversation_id = []
datetime_received = []
has_attachment = []
Senders = []
for i in account.inbox.all().order_by('-datetime_received')[:40]:
if isinstance(i, Message):
if i.datetime_received:
if ((i.datetime_received).year == 2020):
if ((i.datetime_received).month == 7):
if i.conversation_id:
print("conversation id: ", i.conversation_id.id)
conversation_id.append(i.conversation_id.id)
if not i.conversation_id:
conversation_id.append("Not available")
if i.sender:
print("Sender name : ", i.sender.name)
Senders.append(i.sender.name)
if not i.sender:
Senders.append("not available")
if i.datetime_received:
print("Time : ", i.datetime_received.date)
datetime_received.append(i.datetime_received.date)
if not i.datetime_received:
datetime_received.append("not available")
if i.has_attachments:
print("Has attachment: ", i.has_attachments)
has_attachment.append(i.has_attachments)
if not i.has_attachments:
has_attachment.append("not available")
# second approach.....................................................................
items_for_2019 = account.inbox.filter(start__range=(
tz.localize(EWSDateTime(2019, 8, 1)),
tz.localize(EWSDateTime(2020, 1, 1))
))
for i in items_for_2019:
print("")
#third approach.........................................................................
for item in account.inbox.filter(datetime_received__range=(tz.localize(EWSDateTime(2019, 8, 1)),tz.localize(EWSDateTime(2020, 1, 1)))):
print(item.sender.name)
first approach is working for specific month but extremely slow
second and third approach giving wrong output
不胜感激。
start
字段属于 CalendarItem
。它不是 Message
个对象的有效字段。这就是为什么你的第二种方法不起作用。
您的第三种方法应该有效。您看到了哪个输出,您期望什么?
如果您的查询需要很长时间,请尝试使用 .only()
QuerySet 方法限制您为每个项目提取的字段。
要求摘要: 要在用户给定日期范围内访问 Outlook 中特定文件夹中的电子邮件,
ex: all mails from June or all mails from 23-June-2020 to 15-July-2020
到目前为止,我们已经尝试了以下方法,但问题是:
- 日期范围没有给出正确的输出
- 输出时间太长,有时返回超时错误。
代码:
from exchangelib import Credentials, Account, DELEGATE, Configuration, IMPERSONATION, FaultTolerance,EWSDateTime,EWSTimeZone,Message
import datetime
import pandas as pd
new_password = "your password"
credentials = Credentials(username = 'username', password = new_password)
config = Configuration(server ='outlook.office365.com', credentials = credentials)
account = Account(primary_smtp_address ='username', credentials = credentials, autodiscover = False, config = config, access_type = DELEGATE)
#first approach.....................................
conversation_id = []
datetime_received = []
has_attachment = []
Senders = []
for i in account.inbox.all().order_by('-datetime_received')[:40]:
if isinstance(i, Message):
if i.datetime_received:
if ((i.datetime_received).year == 2020):
if ((i.datetime_received).month == 7):
if i.conversation_id:
print("conversation id: ", i.conversation_id.id)
conversation_id.append(i.conversation_id.id)
if not i.conversation_id:
conversation_id.append("Not available")
if i.sender:
print("Sender name : ", i.sender.name)
Senders.append(i.sender.name)
if not i.sender:
Senders.append("not available")
if i.datetime_received:
print("Time : ", i.datetime_received.date)
datetime_received.append(i.datetime_received.date)
if not i.datetime_received:
datetime_received.append("not available")
if i.has_attachments:
print("Has attachment: ", i.has_attachments)
has_attachment.append(i.has_attachments)
if not i.has_attachments:
has_attachment.append("not available")
# second approach.....................................................................
items_for_2019 = account.inbox.filter(start__range=(
tz.localize(EWSDateTime(2019, 8, 1)),
tz.localize(EWSDateTime(2020, 1, 1))
))
for i in items_for_2019:
print("")
#third approach.........................................................................
for item in account.inbox.filter(datetime_received__range=(tz.localize(EWSDateTime(2019, 8, 1)),tz.localize(EWSDateTime(2020, 1, 1)))):
print(item.sender.name)
first approach is working for specific month but extremely slow
second and third approach giving wrong output
不胜感激。
start
字段属于 CalendarItem
。它不是 Message
个对象的有效字段。这就是为什么你的第二种方法不起作用。
您的第三种方法应该有效。您看到了哪个输出,您期望什么?
如果您的查询需要很长时间,请尝试使用 .only()
QuerySet 方法限制您为每个项目提取的字段。