python基于QQ邮箱 邮件发送 代码示例

此文章我用的python版本是:python3.x;

 

脚本代码:

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
 
send_nickname = '依然范儿特西'   # 发件人昵称
send_accout='3151775165@qq.com'  # 发件人邮箱账号
send_pass = '******'   # 发件人邮箱密码

receive_user ='3151775165@qq.com' # 收件人邮箱账号,
title = "代码改变世界"             #邮件的主题或标题


def mail():
    ret=True
    try:
        msg=MIMEText('此处填写邮件内容','plain','utf-8')
        msg['From']=formataddr([send_nickname,send_accout])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['To']=formataddr(["FK",receive_user])            # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['Subject']=title                                 # 邮件的主题,也可以说是标题
        server=smtplib.SMTP_SSL("smtp.qq.com", 465)          # 发件人邮箱中的SMTP服务器,qq端口是465
        server.login(send_accout, send_pass)                 # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(send_accout,[receive_user,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
    except Exception:  # 如果 try 中的语句没有执行,则会执行下面的 ret=False
        ret=False
    return ret
ret=mail()
if ret:
    print("邮件发送成功")
else:
    print("邮件发送失败")

 

邮件发送测试:

python test.py

邮件发送测试: python test.py

 

查看邮件结果:

python查看邮件结果

    A+
发布日期:2018年02月12日 18:27:53  所属分类:Python
最后更新时间:2018-02-12 18:30:34
头像
  • ¥ 1999.0元
  • 市场价:2999.0元
  • ¥ 999.0元
  • 市场价:1599.0元
  • ¥ 68.0元
  • 市场价:168.0元
  • ¥ 69.0元
  • 市场价:69.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: