python exchangelib 同步outlook日历以及联系人

本文介绍了如何使用exchangelib库连接到Outlook.office365.com服务器,实现Exchange协议下的日历事件和联系人信息的同步与操作。通过示例代码展示了获取事件的详细信息,包括主题、创建时间、开始和结束时间等,以及联系人的姓名、昵称、公司名等关键数据。同时,文章提及QQ和163邮箱对Exchange协议的支持情况,探讨了可能的限制和解决方案。
摘要由CSDN通过智能技术生成

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,有知道的小伙伴麻烦留言相告,万分感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值