# !/usr/bin/python3 # coding: utf-8 import smtplib from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr from email.utils import formataddr def format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, "utf-8").encode(), addr)) from_email = "email@qq.com" # 邮箱地址 from_email_pwd = "password" # 邮箱密码 to_email = "to@qq.com" # 接收者邮箱 smtp_server = "smtp.exmail.qq.com" # 协议 msg = MIMEText("<html><body><h3>hello</h3><p>hello, send by python</p></body></html>", "html", "utf-8") msg["From"] = format_addr("%s" % (from_email)) msg["To"] = format_addr("%s" % (to_email)) msg["Subject"] = Header("python email", "utf-8").encode() server = smtplib.SMTP_SSL(smtp_server, port=465) # 腾讯企业邮箱配置(SSL) # server = smtplib.SMTP(smtp_server, port=25) # 网易126邮箱 server.set_debuglevel(1) server.login(from_email, from_email_pwd) server.sendmail(from_email, [to_email], msg.as_string()) server.quit()
网易邮箱配置
开启IMAP/SMTP服务,SMTP服务器: smtp.126.com,设置开启客户端授权密码(代码里的登录密码需使用此授权密码)
smtp_server = "smtp.126.com" # 协议 server = smtplib.SMTP(smtp_server, port=25) # 网易126邮箱
腾讯企业邮箱配置
开启IMAP/SMTP服务
发送服务器:smtp.exmail.qq.com(使用SSL,端口号465),密码为登录密码。