python中Exchange發(fā)郵件
1、安裝exchangelib庫
pip3installexchangelib
2、引入模塊
exchangelib模塊挺多的,其中Account,Credentials用來連接郵箱的,其他的根據(jù)你自身需求來吧,截圖是所有模塊:
3、連接郵箱
登錄郵箱編碼還是比較簡(jiǎn)單的:
credentials=Credentials('域名\用戶名','密碼')
account=Account('郵箱',credentials=credentials,autodiscover=True)
編寫調(diào)用exchangelib庫,發(fā)送郵件
#AuthorKang
fromexchangelibimportDELEGATE,Account,Credentials,Message,Mailbox,HTMLBody
defEmail(to,subject,body):
creds=Credentials(
username='zhoumingkang',
password='帳號(hào)驗(yàn)證的密碼'
)
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='''-----------運(yùn)維報(bào)告------------
CPU使用率:%s
可用內(nèi)存:%s
'''%(cpu,mem)
Email("zhoumingkang@cedarhd.com","主題",message)
以上就是Python中Exchange發(fā)郵件的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)教程請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。