Python的smtplib提供了一种很方便的途径用来发送电子邮件,它有SMTP协议进行简单的封装,可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下:

 from smtplib import SMTP
help(SMTP)

导入SMTP对象,通过help()查看对象的注释,从中找到sendmail()方法的使用说明。

connect(host.port)方法参数说明如下:

host: 指定连接的邮箱服务器。

port:指定连接服务器的端口号。

login(user,password)方法参数说明如下:

user:登录邮箱的用户名

password: 登录邮箱的密码

sendmail(from_addr,to_addr,msg,)方法参数说明如下:

from_addr:邮件发送者地址。

to_addrs:字符串列表,邮件发送地址

msg:发送消息。

quit()方法:用户借宿SMTP回话。

一般发邮件时you两种方式:

方式一:自己邮箱的weby页面(如mail.126.com),输入自己邮箱的用户名和密码登录,打开发邮件页面,填写对方的邮箱地址及邮件标题与正文,完成后点击发送。

方式二:下载安装邮箱客户端(如:OutLook ,Foxmail等)填写邮箱账号,密码及邮箱服务器(如smtp.126.com),一般的邮箱客户端会默认记下这些信息,所这个过程只需填写一次,后面发邮件的过程与方法相同。

通过Python的SMTP对象发邮件则更像方式二,因为需要填写邮箱服务器。在实际发送邮件时,会涉及到很多的需求,例如:邮件的正文格式,是否带图片。邮件

*********************************

一、发送文本/只带有正文的邮件

 # coding: utf-8
import smtplib
import time
from email.mime.text import MIMEText
from email.header import Header sender = 'fengyiru6369@163.com'
receiver = '1194150169@qq.com'
current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
current = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
subject = 'python email test'+current
smtpserver = 'smtp.163.com'
username = 'fengyiru6369@163.com'
password = 'FYRw19222915' # 是授权密码,而不是登录密码 #msg = MIMEText('你好,加油', 'text', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要
msg = MIMEText('你好,加油','plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'Tim<fengyiru6369@163.com>'
msg['To'] = "1194150169@qq.com"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

注:

1.要参考http://blog.csdn.net/huochen1994/article/details/51282093 ,修改163邮箱的授权码,并且登录密码为163邮箱的授权密码

2.注:暂时不知道msg =MIMEText()中text和plain的区别,前者执行是无法显示出正文,后者可以显示出正文。

二、发送HTML形式的邮件

 # coding: utf-8
import smtplib
import time
from email.mime.text import MIMEText
from email.header import Header sender = 'fengyiru6369@163.com'
receiver = '1194150169@qq.com'
current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
current = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
subject = 'python email test'+current
smtpserver = 'smtp.163.com'
username = 'fengyiru6369@163.com'
password = 'FYR19910915' # 是授权密码,而不是登录密码 # msg = MIMEText('你好,加油', 'text', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要
# msg = MIMEText('你好,加油','plain','utf-8')
msg = MIMEText('</pre><h1>你好啊,html</h1><pre>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
   #msg['Subject'] = subject
 msg['From'] = 'Tim<fengyiru6369@163.com>' 
msg['To'] = "1194150169@qq.com"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

三、带图片的HTML邮件

四、带有附件的邮件

 # coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
import os.path
import mimetypes
import time
from email.header import Header # 发送邮箱服务器
smtpserver = 'smtp.163.com'
# 发送邮箱
sender = 'fengyiru6369@163.com'
# 接收邮箱
receiver = '1194150169@qq.com'
# 发送邮箱用户/密码
username = 'fengyiru6369@163.com'
password = 'FYR19910915' # 是授权密码,而不是登录密码 current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
current = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
# 邮箱主题
subject = 'python email test' + current
msg = MIMEMultipart()
# **两种显示邮箱主题的方法
msg['Subject'] = 'test message'
msg['Subject'] = Header(subject, 'utf-8') # 添加附件
part = MIMEApplication(open(r'C:\Users\fyr\Desktop\新建 Microsoft Word 文档 (2).docx', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="123.docx")
msg.attach(part) # 邮件发送
msg['From'] = 'Tim<fengyiru6369@163.com>'
msg['To'] = "1194150169@qq.com"
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

思路是:使用MIMEMultipart来表示这个邮件是多个部分组成的,然后attach各个部分。如果是附件,则add_header加入附件的申明。

在Python中,MIME的这些对象的继承关系如下所示:

一般来说,不会用到MIMEBase,而是直接使用它的继承类。MIMEMultipart有attach方法,而MIMENonMultipart没有,只能被attach。

MIME有很多中类型,如果附件时图片格式,用MIMEImage.如果是音频,用MIMEAudio,等等。

最简便的方法,不管什么类型的附件,都用MIMEApplication,MIMEApplication默认子类型是application/octet-stream.

application/octet-stream表明:“这是个二进制文件,希望你们那边知道怎么处理”,然后哦客户端,比如qq邮箱,收到这个申明后,会根据文件扩展名来猜测。

 #添加附件
#---这是文字部分---
part = MIMEText("乔装打扮,不择手段")
msg.attach(part) #---这是附件部分---
#xlsx类型附件
part = MIMEApplication(open('foo.xlsx','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx")
msg.attach(part) #jpg类型附件
part = MIMEApplication(open('foo.jpg','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.jpg")
msg.attach(part) #pdf类型附件
part = MIMEApplication(open('foo.pdf','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
msg.attach(part) #mp3类型附件
part = MIMEApplication(open('foo.mp3','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
msg.attach(part)

Python之邮件发送的更多相关文章

  1. Python 基于Python实现邮件发送

    基于Python实现邮件发送   by:授客 QQ:1033553122 测试环境: Python版本:Python 2.7   注:需要修改mimetypes.py文件(该文件可通过文章底部的网盘分 ...

  2. 用Python实现邮件发送Hive明细数据

    代码地址如下:http://www.demodashi.com/demo/12673.html 一.需求描述 客户需要每周周一接收特定的活动数据,生成Excel或是CSV文件,并通过邮件发送给指定接收 ...

  3. python SMTP邮件发送(转载)

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  4. python实现邮件发送完整代码(带附件发送方式)

    实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...

  5. python实现邮件发送

    实例补充: #**************************利用STMP自动发送邮件******************************import smtplibsmtp = smtp ...

  6. Python SMTP邮件发送

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块: email负责构造邮件 ...

  7. 基于Python实现邮件发送

    import smtplibfrom email.mime.text import MIMETextemail_host = 'smtp.163.com' # 邮箱地址email_user = 'sz ...

  8. 如何用python进行邮件发送

    使用Python调用邮件服务器发送邮件,使用的协议是SMTP(Simple Mail Transfer Protocol),下图为使用TCP/IP基于SMTP发送邮件的过程示意图: SMTP协议工作原 ...

  9. Python 实现邮件发送功能(初级)

    在我们日常项目中,会经常使用到邮件的发送功能,如何利用Python发送邮件也是一项必备的技能.本文主要讲述利用Python来发送邮件的一些基本操作. 本章主要包含知识点: 邮件发送原理简述即常用smt ...

随机推荐

  1. POJ-3280

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10301   Accepted: 4 ...

  2. java的try后面跟括号

    例子: try (FileReader reader = new FileReader("data.txt")) { ... }catch (IOException io) { . ...

  3. 24.集成ASP.NETCore Identity

    正常的情况下view页面的错误的显示应该是这么去判断的 这里我们就不加判断为了,直接用这个div 显示就可以了.当有错误会自动显示在div内 asp.net core Identity加入进来 这里用 ...

  4. echarts学习的一些笔记

    工具栏组件 Show 是否显示 Feature 具体显示的功能 saveAslmage  保存图片 Restore 还原 dataZoom  缩放视图 magicType 动态类型切换 toltip组 ...

  5. SQL将一个表中的某一列值全部插入到另一个表中

    1.  SQL将一个表中的某一列值全部插入到另一个表中 插入的话: insert into a(col) select col from b; 更新的话: update a set col=selec ...

  6. Unity 5.6中的混合光照(下)

    https://mp.weixin.qq.com/s/DNQFsWpZm-ybIlF3DTAk2A 在<Unity 5.6中的混合光照(上)>中,我们介绍了混合模式,以及Subtracti ...

  7. cf822C(贪心)

    题目链接: http://codeforces.com/problemset/problem/822/C 题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cost(1 ...

  8. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  9. js的Element.scrollIntoView的学习

    1.Element.scrollIntoView()    该方法让当前元素滚动到浏览器窗口的可是区域内: 2.语法: element.scrollIntoView();//等同于element.sc ...

  10. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...