Python中Exchange发邮件

1、安装exchangelib库

pip3installexchangelib

2、引入模块

exchangelib模块挺多的,其中Account, Credentials用来连接邮箱的,其他的根据你自身需求来吧,截图是所有模块:

3、连接邮箱

登录邮箱编码还是比较简单的:

credentials=Credentials('域名\用户名','密码')

account=Account('邮箱',credentials=credentials,autodiscover=True)
编写调用exchangelib库,发送邮件
#AuthorKang

fromexchangelibimportDELEGATE,Account,Credentials,Message,Mailbox,HTMLBody

defEmail(to,subject,body):
creds=Credentials(
username='zhoumingkang',
password='帐号验证的密码'
)
account=Account(
primary_smtp_address='zhoumingkang@cedarhd.com',
credentials=creds,
autodiscover=True,
access_type=DELEGATE
)
m=Message(
account=account,
subject=subject,
body=HTMLBody(body),
to_recipients=[Mailbox(email_address=to)]
)
m.send()

cpu=80
mem=70

message='''-----------运维报告------------<br>
CPU使用率:%s<br>
可用内存:%s<br>
'''%(cpu,mem)

Email("zhoumingkang@cedarhd.com","主题",message)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。