使用SMTP模块发送邮件

  • Post author:
  • Post category:其他




参考教程

此处主要参考风变编程、

参考教程1



参考教程2



实现代码

import smtplib
from email.mime.text import MIMEText
mail_host = "smtp.163.com"
username = "***@163.com"
password = '****'
from_addr = "***@163.com"
to_addr  = "***@qq.com"

# 设置邮件文本信息
msg = MIMEText('A test mail sent by python','plain','utf-8')
msg['Subject'] = 'title'
msg['From'] = from_addr
msg['To'] = to_addr

try:
    smtpObj = smtplib.SMTP(mail_host,timeout=30,port=25)
    smtpObj.starttls()
    # smtpObj.connect(mail_host, 25)
    smtpObj.login(username, password)
    smtpObj.sendmail(from_addr, to_addr, msg.as_string())

    smtpObj.quit()
    print('发送成功')

except smtplib.SMTPException as e:
    print('error',e)



运行结果

发送成功



版权声明:本文为wudi1107原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。