一、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. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  2. springboot+mybatis+springSecurity+thymeleaf

    配置步骤: .pom <dependencies> <dependency> <groupId>org.springframework.security</g ...

  3. ThinkPHP模版时间显示

    <!-- 如果有日期输出,即$data.time不为空且不为0,则格式化时间戳,否则默认当前时间戳,并格式化成日期格式 --> {$data.time|default=time()|dat ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  5. springboot项目支持war部署tomcat

    最近在学校spring boot 在网络上学校到简单的启动spring boot项目,也搭建好了,但时实际情况我的spring boot项目是要发布到tomcat中的,今天,随意打了个war包发布到t ...

  6. python中的变量与对象

    一. 什么是变量 变量就是以前学习的数学中常见的等式x = 3(x是变量,3是变量值),在编程中,变量不仅可以是数学,还可以是任意数据类型 二. 变量的命名规则 变量名必须是英文大小写.数字和_的组合 ...

  7. MRC与ARC混合编程的编译器标记

    如果是MRC项目创建ARC源文件,给这个源文件加上 -fobjc-arc 的编译器标记, 如果是ARC项目创建MRC源文件,给这个源文件加上 -fno-objc-arc 的编译器标记. 步骤: 1. ...

  8. Gitblit的使用

    什么是 Gitblit Gitblit是一个开源的用于管理,查看和提供Git仓库. 它主要设计为希望托管集中存储库的小工作组的工具. Gitblit有什么特点 ... Gitblit部署示例1 日常维 ...

  9. 数据清洗记录,pandas

    pandas数据清洗:http://www.it165.net/pro/html/201405/14269.html data=pd.Series([1,2,3,4]) data.replace([1 ...

  10. [BZOJ5248][多省联测2018]双木棋chess

    bzoj luogu sol 首先,要保证一个格子的左边和上方都放满了棋子,就需要这个点的左上方那个矩形都放满了棋子. 这样放棋子状态就会是一个自左下至右上的轮廓线. 状态数?\(C_{20}^{10 ...