一、smtplib模块:

主要通过SMTP类与邮件系统进行交互。使用方法如下:

1.实例化一个SMTP对象:

  s = smtplib.SMTP(邮件服务地址,端口号)

  s = smtplib.SMTP_SSL(邮件服务地址,端口号)

2.登陆邮件,权限验证:

  s.login(用户名,密码)

3.发送邮件:

  s.sendmail(发件人邮箱,收件人邮箱,发送内容)

4.断开连接:

  s.close()

二、email模块:

  email模块:支持发送的邮件内容为纯文本、HTML内容、图片、附件。email模块中有几大类来针对不同的邮件内容形式,常用如下:

  MIMEText:(MIME媒体类型)内容形式为纯文本、HTML页面。

  MIMEImage:内容形式为图片。

  MIMEMultupart:多形式组合,可包含文本和附件。

每一类对应的导入方式:

  from email.mime.text import MIMEText

  from email.mime.image import MIMEImage

  from email.mime.multipart import MIMEMultipart

三、MIMEText:

  MIMEText(msg,type,chartset)

  msg:文本内容

  type:文本类型默认为plain(纯文本)

   发送HTML格式的时候,修改为html,但同时要求msg的内容也是html的格式。

  chartset:文本编码,中文为“utf-8”

  # 构造TEXT格式的消息

  msg = MIMEText("hello.text","plain","utf-8")

  msg["Subject"] = "xxxxx"

  msg["From"] = "xxxx"

  msg["To"] = "xxxx"

  #发送以上构造的邮件内容要使用as_string将构造的邮件内容转换为string形式。

  s.sendmail("xxx","xxx",msg.as_string)

四、MIMEImage、MIMEMultipart:

  msg = MIMEMultipart()

  #实例化一个文本对象

  msg_sub = MIMEText("hello.text","plain","utf-8")

  #将text消息添加到MIMEMultipart中,作为邮件正文。

  msg.attach(msg_sub)

  #图片作为附件

  import os

  img_datas = open(os.getcwd()+ "/reports/xxxx.png","rb").read()

  msg_img = MIMEImage(img_data)

  msg_img.add_header('Content-Disposition','attachment', filename = "xxxx.png" )

  msg_img.add_header('Content-ID','<0>')

  #将图片添加到MIMEMultiplart中,作为附件发送。

  msg.attach(mag_img)

源代码如下:

发送文本邮件:

 import smtplib
from email.mime.text import MIMEText sender = 'xxxx@qq.com' #发送人邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com' #收件人邮箱 subject = 'python发邮件测试' #主题
content = '这是我使用python smtplib模块和email模块自动发送的邮件' #正文 msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['TO'] = receivers try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('发送成功') except Exception:
print('发送失败')

发送HTML邮件:

 import smtplib
from email.mime.text import MIMEText
from email.header import Header sender = 'xxxx@qq.com' #发件邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com' #收件邮箱 subject = 'python发邮Html邮件测试' #主题 content = """ #内容,HTML格式
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">这是一个链接</a></p>
""" msg = MIMEText(content,'html','utf-8')
# msg['Subject'] = subject
msg['Subject'] = Header(subject,'utf-8')
# msg['From'] = sender
msg['From'] = Header('大傻子','utf-8')
# msg['To'] = receivers
msg['To'] = Header('二愣子','utf-8')
try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Success') except:
print('Send Failure')

发送图片邮件:

 import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart sender = 'xxxx@qq.com'
passwd = 'lkugmgywydhfff'
receivers = 'xxxx@qq.com'
subject = 'python发邮带img的邮件测试' #主题 # 创建一个带附件的实例
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receivers # 创建正文
msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8')) # 创建图片附件
import os
img_file = open(os.getcwd()+"/a4.jpg",'rb').read()
msg_img = MIMEImage(img_file)
msg_img.add_header('Content-Disposition','attachment', filename = "a4.jpg")
msg_img.add_header('Content-ID', '<0>')
msg.attach(msg_img) try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.set_debuglevel(1) #输出发送邮件详细过程
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Succese') except:
print('Send Failure')

发送带附件的邮件:

 import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'xxxxxx@qq.com'  #发件邮箱
passwd = 'lkugmgywydhfff'  # 邮箱授权码
receivers = 'xxxxxx@qq.com'  #收件邮箱 subject = 'python发带附件的邮件测试' #主题
# 创建一个带附件的实例
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receivers #创建正文,将文本文件添加到MIMEMultipart中
msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8')) #构造附件1,传送当前目录下 文件
att1 = MIMEText(open('testdata.xlsx','rb').read(),'base64','utf-8') # rb以二进制方式读取
# att1["Content-Type"] = 'application/octet-stream'
# filename为附件名称,可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename = "testdata.xlsx" '
#将附件添加到MIMEMultipart中
msg.attach(att1) #构造附件2
att2 = MIMEText(open('db.cfg','rb').read(),'base64','utf-8')
# att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename = "db.cfg" '
#将附件添加到MIMEMultipart中
msg.attach(att2) try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.set_debuglevel(1) #输出发送邮件详细过程
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Succese') except:
print('Send Failure')

Python使用SMTP模块、email模块发送邮件的更多相关文章

  1. python使用smtplib和email库发送邮件

    国内很多服务器提供商都默认禁止了smtp默认的25端口服务,而启用465端口发送邮件 在smtplib库中直接调用SMTP_SSL就是默认使用465端口 示例代码如下: def send_eamil( ...

  2. 利用Python的smtplib和email发送邮件

    原理 网上已经有了很多的教程讲解相关的发送邮件的原理,在这里还是推荐一下廖雪峰老师的Python教程,讲解通俗易懂.简要来说,SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本 ...

  3. python email ==> send 发送邮件 :) [smtplib, email 模块]

    关于Email的预备知识: 原贴地址:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343480.html ############ ...

  4. 使用python调用email模块发送邮件附件

    使用python调用email模块实现附件发送 需要模块: import datetime import time import sys import mimetypes import smtplib ...

  5. Python_使用smtplib和email模块发送邮件

    [http://blog.csdn.net/menglei8625/article/details/7721746] SMTP (Simple Mail Transfer Protocol) 邮件传送 ...

  6. python email模块

    python email模块 官方文档 email模块 电子邮件包是一个用于管理电子邮件消息的库.它的特殊设计不用于向SMTP (RFC 2821).NNTP或其他服务器发送任何电子邮件消息;这些是模 ...

  7. Python 通过 SMTP 发送邮件

    Python版本:Python3.5.2 简介 SMTP是发送邮件的协议,Python 内置对 SMTP 的支持,可以发送纯文本邮件.HTML 邮件以及带附件的邮件. Python 对 SMTP 支持 ...

  8. python学习笔记8-邮件模块

    我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下: impo ...

  9. python实现邮件接口——smtplib模块

    1. 思路 使用脚本发送邮件的思路其实和客户端发送邮件一样,过程都是: 登录 —> 写邮件 —> 发送 只不过通过脚本发送时我们需要考虑到整个过程的方方面面.以下为思路导图: 2. Pyt ...

随机推荐

  1. codeforces 155D 质数

    题意:有编号1到n的n台机器,有m次操作,操作为开启或关闭机器,成功开启机器k的条件为k和所有已经开启的机器编号互质. 思路:vis[i]数组存放占领i这个位置的机器编号,因为所有开启的机器的编号互质 ...

  2. 【bzoj4765】普通计算姬(双重分块)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4765 这道题已经攒了半年多了...因为懒,一直没去写...所以今天才把这道题写出来... ...

  3. linux基础(5)-用户及权限

    用户与组 用户:使用linux时,需要以用户的身份登陆 组    :用来方便组织管理用户   用户种类 -root用户(ID为0的用户为root用户) -系统用户(1-499) -普通用户(500以上 ...

  4. rpm卸载软件error preun

    这两天,使用ipvsadm -ln总是显示空. 后来,使用strace ipvsadm -ln定位 看来,是ipvsadm模块有问题,卸载了再重新安装吧,结果出现这种问题. 从来没遇到这种问题: er ...

  5. java创建多线程的三种方式

    /***************************继承Thread类创建多线程************************/ public class FirstThread extends ...

  6. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. Python OS导入一个文件夹所有文件

    import os path = 'F:/save_file/seminarseries/' for root, dirs, files in os.walk(path): print(root) 这 ...

  8. keystone cache

    http://docs.openstack.org/juno/config-reference/content/section_keystone.conf.html http://docs.opens ...

  9. MySql基础学习-Sql约束

    1.主键约束(PRIMARY KEY) 主键 (PRIMARY KEY)是用于约束表中的一行,作为这一行的唯一标识符,在一张表中通过主键就能准确定位到一行,因此主键十分重要.主键不能有重复且不能为空. ...

  10. dp1--乘积最大

    dp1--乘积最大 一.心得 1.用excel填数组很方便 2. dp就是填表 找状态就是缩小规模 找状态转移方程就是 找状态的最后一次关系 二.题目 8782:乘积最大 查看 提交 统计 提问 总时 ...