使用SMTP发送邮件

# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
msg_from = 'xxxx@163.com'  # 发送方邮箱
passwd = 'xxxx'  # 填入发送方邮箱的授权码(填入自己的授权码,相当于邮箱密码)
msg_to = ['914101530@qq.com']  # 收件人邮箱
subject = "新年快乐!"  # 主题
content = "我是邮件的内容,祝你新年快乐。"
msg = MIMEText(content)
# 放入邮件主题
msg['Subject'] = subject
msg['From'] = msg_from
try:
    # 通过ssl方式发送,服务器地址,端口
    s = smtplib.SMTP_SSL("smtp.163.com", 465)
    # 登录到邮箱
    s.login(msg_from, passwd)
    # 发送邮件:发送方,收件方,要发送的消息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()

效果如下

Last modification:February 1st, 2020 at 09:54 pm
正在沿街乞讨中……