outlook smtp 发送邮件

前提条件

开通 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

如何创建应用密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值