Python实现exchangelib发送带附件的邮件

来了哈!
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 发邮件,带附件

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

BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
logging.captureWarnings(True)

if __name__ == '__main__':
    creds = Credentials(r'域名\姓名', '密码')
    # config = Configuration(server="mail.域名.com", credentials=creds, auth_type=NTLM)
    account = Account(primary_smtp_address='姓名@域名.com', credentials=creds, autodiscover=True, access_type=DELEGATE)
    with open(os.path.abspath("../res/noteB.txt"), "r", encoding="utf-8") as f:
        body = f.read()
    m = Message(account=account, subject="中奖名单", body=HTMLBody(body), to_recipients=[Mailbox(email_address='[email protected]')])
    # 附件加"rb"
    with open(os.path.abspath(r"../res/noteA.txt"), "rb") as f:
        cont = f.read()
    attchF = FileAttachment(name='名单.txt', content=cont)
    m.attach(attchF)
    m.send_and_save()

完了哈! 附件文件自己创造,正文body可以是HTML文件哦。

 

 

 

另外友情推荐给有需要的同学:

 http://www.ai-course.cn/

 

你可能感兴趣的:(python)