Python使用exchangelib发送邮件

上一篇 / 下一篇  2021-09-27 17:21:05 / 个人分类:SoftTest

51Testing软件测试网%a^b'XQ0\
安装 exchangelib :
(使用国内清华的镜像源)

Q5mm0o2lrVu R0
代码如下:
51Testing软件测试网w'l}N]%c0P
# coding=utf-8
#
# Created on 2021/9

V&r u0\ O1S` MT0

"_#j O u `'fq4d0
from exchangelib import DELEGATE, Account, Credentials, Configuration, NTLM, Message, Mailbox, HTMLBody
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter

)Vg){ Kc[E0
51Testing软件测试网8R@xuX8[;s&oX
#此句用来消除ssl证书错误,exchange使用自签证书需加上
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
51Testing软件测试网U0e9i%M']YCy4z

E)]j9rkk0
# 输入你的域账号如example\leo
cred = Credentials(r'EXAMPLE\leo', '输入你的密码')
51Testing软件测试网P-U TP@`'D
config = Configuration(server='输入邮箱服务器网页地址', credentials=cred, auth_type=NTLM)
a = Account(
    primary_smtp_address='输入你要绑定的邮箱名(leo@example.com)', config=config, autodiscover=False, access_type=DELEGATE
)
51Testing软件测试网Gn]lY2o
# 此处为用来发送html格式邮件的文件路径
with open(r'C:\Users\leo\Desktop\1.html') as f:
    msg = f.read().decode('utf-8')
51Testing软件测试网 zsihA3L5X
m = Message(
    account=a,
    folder=a.sent,
    subject=u'测试邮件',
    body=HTMLBody(msg),
    to_recipients=[Mailbox(email_address='输入你要绑定的邮箱名(leo@example.com)')]
)
m.send_and_save()

TAG:

 

评分:0

我来说两句

Open Toolbar