Exchange协议:
Exchange协议可供用户同步邮件、联系人、日历及其他所有Exchange对象。由于这个协议需要部署Exchange服务器,因此通常为公司或者机构账号所用。它的好处是:全邮件同步;邮件保存在服务器上;支持绝大部分移动设备、联系人、日历和数据同步;在服务器域中邮件可撤回并修改。坏处是必须部署昂贵的Exchange服务器。
Exchange大致工作原理:
https://域名/EWS/Exchange.asmx页面提供出来的服务,这也是为什么需要提供域名了,跟pop,imap协议不一样,然后跟部署的服务器进行交互,从而获取数据exchangelib文档传送门
outlook貌似又几个地址:outlook.office365.com,imap-mail.outlook.com,s.outlook.com等等
话不多说,上代码!!!
Outlook 简单实现:
from exchangelib import Credentials, Account, DELEGATE, Configuration, NTLM, Message, Mailbox, HTMLBody
def connect(server, email, username, password):
"""
Get Exchange account connection with server
"""
creds = Credentials(username=username, password=password)
config = Configuration(server=server, credentials=creds)
return Account(primary_smtp_address=email, autodiscover=False, config = config, access_type=DELEGATE)
server = 'outlook.office365.com'
email = 'xxxxxxxxxxx' # 邮箱地址 xxx@outlook.com
username = 'xxxxxxxxxxx' # 同上
password = 'xxxxxxxxxxx' # 密码 不是授权码
account = connect(server, email, username, password)
for event in account.calendar.all():
print(type(event))
print(event.subject)
print('datetime_created is {}'.format(event.datetime_created.timestamp()))
print('last_modified_time is {}'.format(event.last_modified_time.timestamp()))
print('event._start_timezone is {}'.format(type(event._start_timezone)))
print('event._start_timezone is {}'.format(event._start_timezone))
print('is_meeting is {}'.format(event.is_meeting))
print('is_cancelled is {}'.format(event.is_cancelled))
print('organizer is {}'.format(event.organizer))
print('start is {}'.format(event.start))
print('end is {}'.format(event.end))
print('location is {}'.format(event.location))
print('duration is {}'.format(event.duration))
print('attachments is {}'.format(event.attachments))
print('has attachments is {}'.format(event.has_attachments))
print('is_draft is {}'.format(event.is_draft))
print('is_all_day is {}'.format(event.is_all_day))
print('------------------------------------------------------------------------------------')
for contact in account.contacts.all():
print(type(contact))
print('display_name is {}'.format(contact.display_name))
print('nickname is {}'.format(contact.nickname))
print('middle_name is {}'.format(contact.middle_name))
print('company_name is {}'.format(contact.company_name))
print('email_addresses is {}'.format(type(contact.email_addresses)))
print('email_addresses is {}'.format(contact.email_addresses[0].email))
print('physical_addresses is {}'.format(contact.physical_addresses))
print('phone_numbers is {}'.format(contact.phone_numbers[0].phone_number))
print('birthday is {}'.format(contact.birthday))
print('job_title is {}'.format(contact.job_title))
print('assistant_name is {}'.format(contact.assistant_name))
print('------------------------------------------------------------------------------------')
结果如下,数据已经打码了
写在最后:
QQ 跟 163的没有找到地址,163是i.163.com但貌似并不能用,QQ是ex.qq.com(ex.exmail.qq.com,企业邮箱),然而我得到了下面的结果:
使用Window10 自带的邮箱,采用exchange协议也能同步下来,用wireshark抓包,发现请求的IP地址是183.3.235.185,跟ex.qq.com解析出来的IP一样,也有人说QQ只支持移动端的exchange,有知道的小伙伴麻烦留言相告,万分感谢!