from exchangelib import Credentials, Account, DELEGATE, Configuration, NTLM, Message, Mailbox, HTMLBody
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
import urllib3
urllib3.disable_warnings () # 取消SSL安全连接警告
# # Tell exchangelib to use this adapter class instead of the default
# # exchangelib provides a sample adapter which ignores TLS validation errors.
# # Use at own risk. NTML is NT LAN Manager.
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
cred = Credentials ('域\\用户名', '密码') # 用户名不需要填写后缀
config = Configuration (
server='邮箱服务器', # 例如:mail.****.com
credentials=cred,
auth_type=NTLM
)
account = Account (
primary_smtp_address='你的邮箱地址', # 例如:ad@test.com
config=config,
autodiscover=False,
access_type=DELEGATE
)
# m = Message (
# account=account,
# subject='这是测试标题', # 标题
# body=HTMLBody ('这是内容'), # 内容
# to_recipients=[Mailbox (email_address='669216072@qq.com')] # 要发送的邮箱地址
# )
# m.send () # 发送邮件
def Email(to, subject, body): # 创建函数用于方便调用发送
m = Message(
account=account,
subject=subject,
body=HTMLBody(body),
to_recipients = [Mailbox(email_address=to)]
)
m.send()
Email("669216072@qq.com", "标题", "内容") # 测试调用发送邮件
原创:https://blog.csdn.net/weixin_40841831/article/details/94064765 经过我小量修改,测试正常使用