python / 练习级源码 · 02/20/2023 0

python SMTP邮件(文本版)

import smtplib
import ssl
from email.message import EmailMessage

#无需安装第三方库
key='………………'      #换成你的QQ邮箱SMTP的授权码(QQ邮箱设置里)
EMAIL_ADDRESS='787523621@qq.com'      #换成你的邮箱地址,欢迎给我发邮件
EMAIL_PASSWORD=key
smtp=smtplib.SMTP('smtp.qq.com',25)
context=ssl.create_default_context()
sender=EMAIL_ADDRESS                                         #发件邮箱
receiver='1094598776@qq.com'
                                      #收件邮箱

subject="python email subject"
body="Hello,this is an email sent by python!587||456"
msg=EmailMessage() #msg 是一个对象,其中有subject sender receiver 三个属性 邮件内容是一个方法 set_content()
msg['subject']=subject       #邮件主题
msg['From']=sender
msg['To']=receiver
msg.set_content(body)         #邮件内容

with smtplib.SMTP_SSL("smtp.qq.com",587,context=context) as smtp: #加密传输方法
    smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD)
    smtp.send_message(msg)