前提条件
开通 app password
开通 smtp 服务
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_html_email_smtp(sender_email, sender_password, recipient_email, subject, html_content):
# Create the message container
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = recipient_email
# Attach the HTML content
msg.attach(MIMEText(html_content, 'html'))
# Send the message via SMTP server
with smtplib.SMTP('smtp.office365.com', 587) as server:
server.starttls() # Secure the connection
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, msg.as_string())
# Let's test this!
send_html_email_smtp(
'your_email@outlook.com',
'your_password',
'recipient_email@example.com',
'Hello from Python!',
'<h1>Hi there!</h1><p>Hi! This is a test email from your friendly Python script.</p>'
)
参考
https://medium.com/@jinglemind.dev/lets-send-outlook-emails-using-smtp-and-graph-api-a3001e136359
如何创建应用密码