# 这是一个在用户标准日历中创建日程安排的示例,如果需要访问非标准日历,选择一个与account.folders[Calendar]不同的文件夹即可
#
# 可以新建、更新和删除日程:
from exchangelib import Account, CalendarItem, Message, Mailbox, FileAttachment, HTMLBody
from exchangelib.items import SEND_ONLY_TO_ALL, SEND_ONLY_TO_CHANGED
from exchangelib.properties import DistinguishedFolderId
a = Account(...)
item = CalendarItem(folder=a.calendar, subject='foo')
# 注意:我在实际操作时要求指定'start'、'end'属性
item.save() # 保存操作将给日程实例的'id'和'changekey'赋值
item.save(send_meeting_invitations=SEND_ONLY_TO_ALL) # 向与会者发送会议邀请
# 更新一个属性,所有属性都有对应的Python类型
item.subject = 'bar'
# 打印'CalendarItem'类上的所有可用字段。请注意,有些字段是只读的,或者在保存或发送后是只读的,有些字段在旧版本的Exchange上是不支持的。
print(CalendarItem.FIELDS)
item.save() # 当item有item_id后,save()函数将更新变化的属性
item.save(update_fields=['subject']) # 只更新指定字段,参数为列表可指定多个字段
item.save(send_meeting_invitations=SEND_ONLY_TO_CHANGED) # 仅向与会者更改发送邀请
item.delete() # 完全删除
item.delete(send_meeting_cancellations=SEND_ONLY_TO_ALL) # 向所有与会者发送取消通知
item.soft_delete() # 删除,但在“可恢复项目”文件夹中保留一个副本
item.move_to_trash() # 移到垃圾邮件文件夹
item.move(a.trash) # 移到垃圾邮件文件夹
item.copy(a.trash) # 拷贝到垃圾邮件文件夹
item.archive(DistinguishedFolderId('inbox')) # 将项目存档到存档邮箱的收件箱
# 你也可以发电子邮件。如果不需要本地副本:
m = Message(
account=a,
subject='Daily motivation',
body='All bodies are beautiful',
to_recipients=[
Mailbox(email_address='anne@example.com'),
Mailbox(email_address='bob@example.com'),
],
cc_recipients=['carl@example.com', 'denice@example.com'], # Simple strings work, too
bcc_recipients=[
Mailbox(email_address='erik@example.com'),
'felicity@example.com',
], # Or a mix of both
)
m.send()
# 或者在已发送文件保存一个副本:
m = Message(
account=a,
folder=a.sent,
subject='Daily motivation',
body='All bodies are beautiful',
to_recipients=[Mailbox(email_address='anne@example.com')]
)
m.send_and_save()
# 同样,您可以回复和转发存储在邮箱中的邮件(即邮件具有项目ID)。
m = a.sent.get(subject='Daily motivation')
m.reply(
subject='Re: Daily motivation',
body='I agree',
to_recipients=['carl@example.com', 'denice@example.com']
)
m.reply_all(subject='Re: Daily motivation', body='I agree')
m.forward(
subject='Fwd: Daily motivation',
body='Hey, look at this!',
to_recipients=['carl@example.com', 'denice@example.com']
)
# 您还可以编辑回复或转发的草稿
forward_draft = m.create_forward(
subject='Fwd: Daily motivation',
body='Hey, look at this!',
to_recipients=['carl@example.com', 'denice@example.com']
).save(a.drafts) # gives you back the item
forward_draft.reply_to = ['erik@example.com']
forward_draft.attach(FileAttachment(name='my_file.txt', content='hello world'.encode('utf-8')))
forward_draft.send() # 现在我们的转发邮件有一个额外的回复,还有一个额外的附件。
# EWS区分纯文本和HTML正文内容。如果要发送HTML正文内容,请使用HTMLBody帮助类。客户端会将其视为HTML并正确显示正文:
item.body = HTMLBody('<html><body>Hello happy <blink>OWA user!</blink></body></html>')
5. Exchangelib3.2官方文档 —— 新建、更新、删除、发送、移动和归档邮件
最新推荐文章于 2024-07-10 10:58:48 发布