Python利用QQ邮箱发送邮件的实现方法(分享)

yipeiwu_com5年前Python基础

废话不多说,直接上代码

Python2.7

#!/usr/bin/env python2.7
# -*- coding=utf-8 -*-

import smtplib
from email.mime.text import MIMEText
_user = "648613081@qq.com"
_pwd = "这里改成你的授权码"
_to  = "648613081@qq.com"

msg = MIMEText("this is a email from python,ha ha ha ...")
msg["Subject"] = "这里是主题"
msg["From"] = _user
msg["To"] = _to

try:
  s = smtplib.SMTP_SSL("smtp.qq.com", 465)
  s.login(_user, _pwd)
  s.sendmail(_user, _to, msg.as_string())
  s.quit()
  print "发送成功"
except s.smtplib.SMTPException, e:
  print "发送失败"

Python3.6

#!/usr/bin/env python3.6
# -*- coding=utf-8 -*-

import smtplib
from email.mime.text import MIMEText
_user = "648613081@qq.com"
_pwd = "这里改成你的授权码"
_to  = "648613081@qq.com"

msg = MIMEText("this is a email from python,ha ha ha ...")
msg["Subject"] = "这里是主题"
msg["From"] = _user
msg["To"] = _to

try:
  s = smtplib.SMTP_SSL("smtp.qq.com", 465)
  s.login(_user, _pwd)
  s.sendmail(_user, _to, msg.as_string())
  s.quit()
  print ("发送成功")
except (s.smtplib.SMTPException, e):
  print ("发送失败")

以上这篇Python利用QQ邮箱发送邮件的实现方法(分享)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 运算符 供重载参考

二元运算符 特殊方法 + __add__,__radd__ - __sub__,__rsub__ * __mul__,__rmul__ / __div__...

Python异常的检测和处理方法

捕获异常 # 对数字变量使用append操作 a = 123 a.apppend(4) 执行这个程序时,会抛出: AttributeError: 'int' object h...

python3 mmh3安装及使用方法

python3 mmh3安装及使用方法

mmh3安装方法 哈希方法主要有MD、SHA、Murmur、CityHash、MAC等几种方法。mmh3全程murmurhash3,是一种非加密的哈希算法,常用于hadoop等分布式存储...

python机器学习理论与实战(五)支持向量机

python机器学习理论与实战(五)支持向量机

       做机器学习的一定对支持向量机(support vector machine-SVM)颇为熟悉,因为在深...

pyshp创建shp点文件的方法

如下所示: # coding:utf-8 import shapefile w = shapefile.Writer() w.autoBalance = 1 w = shapef...