黑马程序员技术交流社区

标题: python 实现SMTP发送邮件(二) [打印本页]

作者: python萌新    时间: 2019-9-19 15:26
标题: python 实现SMTP发送邮件(二)
python 实现SMTP发送邮件(二)-发送多人邮件


[Python] 纯文本查看 复制代码
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 配置邮箱服务器
smtpserver = "smtp.163.com"

# 用户/密码
user = "admin@163.com"
password = "123456"

# 发送者邮箱
sender = "admin@163.com"

# 接收者邮箱
receiver = ["123456@163.com","234567@qq.com"]

# 邮件主题
subject = "Python-email2"

msg = MIMEText('<html><h1>你好!</h1></html>', "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")


# 多人接收邮件,直接显示下面账号的名字
msg['From'] = "admin@163.com"
msg['To'] = "123456@163.com;234567@qq.com"


if __name__ == '__main__':
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver, 25)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

需要注意添加上msg[“From”]和msg[“To”],而且要跟接收方邮箱相同(receiver),否则可能会报以下错误:
[Shell] 纯文本查看 复制代码
smtplib.SMTPDataError: (554, b'DT:SPM 163 smtp3,G9xpCgAnchCrbkFdS_YZAA--.189S2 1564569259,please see http://mail.163.com/help/help_spam_16.htm?ip=157.122.54.188&hostid=smtp3&time=1564569259')






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2