Python QQMail SMTP发邮件

in Python with 0 comment

Tip:3月5日,“电子邮件之父”,美国计算机程序员汤姆林森去世,享年74岁,他让Email有了「@」。

「Python中使用QQ邮箱SMTP服务器发邮件」代码:

#-*- coding:utf-8 -*-
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))

# 输入Email地址和口令:
from_addr = "your email address"
password = "your password" #有独立密码的输独立密码
# 输入SMTP服务器地址:
smtp_server = "smtp.qq.com"
# 输入收件人地址:
to_addr = "target email address"

msg = MIMEText('<html><body><p>邮件正文内容</p></body></html>', 'html', 'utf-8')
msg['From'] = _format_addr(u'发件人姓名<%s>' % from_addr)
msg['To'] = _format_addr(u'收件人姓名<%s>' % to_addr)
msg['Subject'] = Header(u'邮件标题', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25,并非官方提供的465或587
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
Responses
选择表情选择表情