python 发送exchange邮件

1、首先下载一个神器exchangelib,操作 pip install exchangelib

2、然后只需要将下面的信息改成你的信息就可以了

from exchangelib import Account, Message, Mailbox, HTMLBody, ServiceAccount
#忽略警告
import urllib3
urllib3.disable_warnings()

#报错处理:ConnectionError: HTTPConnectionPool(host='mail.xxx.com', port=443): Max retries exceeded with url:
# import socket
# socket.create_connection((host, port), timeout=10)

def Email(to, subject, body):
creds = ServiceAccount(username='域名', password='密码')
account = Account('发送的邮箱', credentials=creds, autodiscover=True)
m = Message(account=account,subject=subject,body=HTMLBody(body),
to_recipients = [Mailbox(email_address=to)]
)
m.send()

Email("接收的邮箱", "邮件标题", "邮件内容")
 
原文地址:https://www.cnblogs.com/zpw-1/p/9758705.html