Exchangelib库不会下载html格式的电子邮件正文

时间:2019-04-11 12:28:17

标签: python exchangelib

我已经编写了代码,使用Exchangelib库将MS Exchange Server电子邮件正文下载为html,但是它下载的HTML内容没有<>括号

import datetime
from exchangelib import ServiceAccount, Account, Configuration, DELEGATE
from exchangelib import EWSDateTime, EWSTimeZone, EWSDate, 

server = 'server url'
username = 'username'
password= 'password'
credentials = ServiceAccount(username=username, password=password)
config = Configuration(server=server, credentials=credentials)
account = Account(
    primary_smtp_address='xyz@gmail.com',
    config=config, credentials=credentials,
    autodiscover=False,
    access_type=DELEGATE
)

# to fetch 6 days before emails
tz = EWSTimeZone.localzone()
end = tz.localize(EWSDateTime.combine(EWSDate.today(), datetime.time(0)))
start = end - datetime.timedelta(days=6)
for item in account.inbox.filter(datetime_received__range=(start, end)):
    emailbody = item.body
    with open('test.html', 'w', encoding='utf-8') as fdata:   
        fdata.write(emailbody)

预期结果:

<html><head>....</head></html>

实际输出:

html head .../head /html

1 个答案:

答案 0 :(得分:0)

这可能就是从服务器接收正文的方式。如果您enable debug logging,则可以看到身体的实际外观。