当前位置: 首页 > 图灵资讯 > 行业资讯> Python中Exchange发邮件

Python中Exchange发邮件

来源:图灵python
时间: 2024-08-08 16:06:38

1、安装exchangelib库

pip3installexchangelib

2、引入模块

exchangelib模块相当多,其中Account, Credentials是用来连接邮箱的,其他的则是根据自己的需要。截图是所有模块:

3、连接邮箱

登录邮箱编码相对简单:

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

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)

以上是Python中Exchange发邮件的方法,希望对大家有所帮助。更多Python学习指导:python基础教程