1. 程式人生 > >python 使用exchange傳送郵件

python 使用exchange傳送郵件

安裝庫exchangelib

pip install exchangelib

指令碼內容

# coding=utf-8
#
# Created on 2018/2/


from exchangelib import DELEGATE, Account, Credentials, Configuration, NTLM, Message, Mailbox, HTMLBody
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter


#此句用來消除ssl證書錯誤,exchange使用自簽證書需加上
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter


# 輸入你的域賬號如example\leo
cred = Credentials(r'EXAMPLE\leo', '輸入你的密碼') config = Configuration(server='輸入郵箱伺服器網頁地址', credentials=cred, auth_type=NTLM) a = Account( primary_smtp_address='輸入你要繫結的郵箱名([email protected])', config=config, autodiscover=False, access_type=DELEGATE ) # 此處為用來發送html格式郵件的檔案路徑 with open(r'C:\Users\leo\Desktop\1.html'
) as f: msg = f.read().decode('utf-8') m = Message( account=a, folder=a.sent, subject=u'測試郵件', body=HTMLBody(msg), to_recipients=[Mailbox(email_address='輸入你要繫結的郵箱名([email protected])')] ) m.send_and_save()