文件形式的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要

msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

HTML形式的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('

你好

','html','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

带图片的HTML邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

msgText = MIMEText('Some HTML text and an image.
good!','html','utf-8')

msgRoot.attach(msgText)

fp = open('h:\\python\\1.jpg', 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

msgImage.add_header('Content-ID', '')

msgRoot.attach(msgImage)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

带附件的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

#构造附件

att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment; filename="1.jpg"'

msgRoot.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

群邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = ['***','****',……]

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','plain','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

各种元素都包含的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

# Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart('alternative')

msg['Subject'] = "Link"

# Create the body of the message (a plain-text and an HTML version).

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"

html = """\

Hi!

How are you?

Here is the link you wanted.

"""

# Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, 'plain')

part2 = MIMEText(html, 'html')

# Attach parts into message container.

# According to RFC 2046, the last part of a multipart message, in this case

# the HTML message, is best and preferred.

msg.attach(part1)

msg.attach(part2)

#构造附件

att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment; filename="1.jpg"'

msg.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

基于SSL的邮件

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要

msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.set_debuglevel(1)

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

python发邮件实例_python发邮件实例相关推荐

  1. python 邮件解析_Python解析邮件

    邮件的解析是个大课题,远超一般人的预期.它远比发送邮件和接收邮件要复杂的多的多. 这就是为什么网上中文外文搜邮件的问题,绝大多数都是讲发送的而讲接收的很少. 发送邮件好说,接收和下载邮件也好说.关键是 ...

  2. python邮件模块_Python收发邮件模块,用,来,发送,接收

    用Python来发送接收邮件模块 python实现发送和接收邮件功能主要用到poplib和smtplib模块. poplib用于接收邮件,而smtplib负责发送邮件. # -- coding :ut ...

  3. python 类 实例_Python类的实例详解

    类(class)是一个用户自定义类型,开发者可以将其实例化以获得实例(instance),实例表示这种类型的对象.在Python中,类就是对象,开发者可以像对其他对象那样处理函数,可以在调用函数时传递 ...

  4. python温度转换实例_Python温度转换实例分析

    Python温度转换实例分析 本文主要研究的是Python语言实现温度转换的相关实例,具体如下. 代码如下: #TempConvert.py val=input("请输入带有温度表示符号的温 ...

  5. python爬取邮件内容_python 接收邮件获取邮件内容

    收取邮件有两种协议,POP3和IMAP,POP3相对于IMAP功能较少无法对邮件进行更深层次的操作,因此本文使用IMAP协议收取邮件.python提供了很多收邮件的模块,本文使用imaplib来接收邮 ...

  6. python发邮件实例_python 发邮件实例

    1 importsmtplib2 from email.mime.multipart importMIMEMultipart3 from email.mime.text importMIMEText4 ...

  7. python是什么邮箱_python发邮件详解,smtplib和email模块详解

    在介绍具体的实现python发邮件的具体操作之前,我觉得有必要介绍下SMTP,更有助于理解python发邮件的实现原理.SMTP协议属于TCP/IP协议簇,即简单邮件传输协议,它是一组用于由源地址到目 ...

  8. python邮件发送_Python实现邮件发送

    使用smtplib模块发送邮件,它对smtp协议进行了简单的封装. smtp协议的基本命令包括: HELO 向服务器标识用户身份 MAIL 初始化邮件传输 mail from: RCPT 标识单个的邮 ...

  9. python字符串处理编程实例_Python字符串处理实例详解

    干货大礼包!21天带你轻松学Python(文末领取更多福利) 点击查看课程视频地址 本课程来自于千锋教育在阿里云开发者社区学习中心上线课程<Python入门2020最新大课>,主讲人姜伟. ...

最新文章

  1. UIExtendedEdge
  2. WCF分布式开发常见错误(25):The certificate 'CN=WCFHTTPS' must have a private key
  3. appium怎么操作物理返回键_这些Appium常用元素定位技巧,你掌握了几种?
  4. [20180826]四校联考
  5. JavaScript与二进制数据的恩怨情仇
  6. android Fragments (Android官方文档中文版)
  7. 计算机更换桌面图片,桌面图片高清怎么换?桌面图片怎么美化?
  8. 微信管理系统-联络易
  9. 计算机硬件关系密切,与计算机硬件关系最密切的软件是.
  10. jpg图片怎么转jpeg格式?赶快进来学习下新操作
  11. 温故而知新---jquery(jq)进阶篇
  12. 机器学习神器Scikit-Learn保姆级入门教程
  13. BZOJ_1778_[Usaco2010_Hol]_Dotp_驱逐猪猡_(期望动态规划+高斯消元+矩阵)
  14. 个人常用iOS第三方库以及XCode插件介绍
  15. 嵌入式经典面试题总结
  16. 毕业设计-基于大数据的电影推荐系统-python
  17. iOS内存管理 —— 自动释放池和runloop
  18. 乱码问题之文件,文本文件以及编码
  19. 计算机毕业设计基于Springboot+vue口腔牙科诊所管理系统
  20. Python django 公司OA管理系统

热门文章

  1. 谷歌chrome浏览器提示“喔唷 崩溃啦”的解决方案
  2. 【亲测有效】无法定位链接器!请检查 tools\link.ini 中的配置是否正确的解决方案
  3. python 保证所有数据能够显示,而不是用省略号表示,不以科学计数显示
  4. 芝柏 bmw oracle,魅力十足的潜水腕表
  5. mysql 权限管理无效_mysql 权限控制笔记
  6. JQ实现列表增删以及上下移动
  7. CSS3属性选择器(CSS3)
  8. linux之解决libipopt.so.1: Cannot open shared object file
  9. C#文件夹的创建和定期删除
  10. spring-mvc默认首页配置