一、发送文本邮件

可能的问题1.:需要注意,目前QQ邮箱来讲,不能收到完整的邮件,即有些内容不能显示,最好全部使用网易邮箱:

可能的问题2.:在以往的文本邮件发送中,只写了

msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')

目前来讲,需要有三个参数,即

#  中文需要参数utf-8
msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com'

文本代码参考(写代码的时候是本人的邮箱,在此已将邮箱更改,复制的时候需要注意)

# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header # 发送邮箱
sender = 'abc@163.com' # 接收邮箱
receiver = 'def@163.com' # 发送邮件主题
subject = 'Python email testing' # 发送邮箱服务器
smtpserver = 'smtp.163.com' # 发送邮箱用户名/密码
username = "abc@163.com"
password = "" # 中文需要参数utf-8
msg = MIMEText('hello,world,中文邮件测试', 'text', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

test_text_mail

二、发送HTML邮件

将参数内容修改即可:如下

#  HTML形式的文件内容
msg = MIMEText('<html><h1>你好!!欢迎使用html格式</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
# msg['From'] = 'lemon<abc@163.com>'
# msg['To'] = 'def@163.com'

其他内容,与上述代码一致;

三、发送img邮件

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'abc@163.com'
receiver = 'def@163.com'
subject = 'Python email image testing'
smtpserver = 'smtp.163.com'
username = "abc@163.com"
password = "" # 发送图片附件的邮件
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' txt = MIMEText("这是中文的邮件内容哦","plain",'utf-8')
msg.attach(txt) file1 = r"E:\images\2.jpg"
image = MIMEImage(open(file1, 'rb').read())
image.add_header('Content-ID', '<image1>')
msg.attach(image) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

test_img_mail

显示如下:

新内容:

msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' txt = MIMEText("这是中文的邮件内容哦","plain",'utf-8')
msg.attach(txt) file1 = r"E:\images\2.jpg"
image = MIMEImage(open(file1, 'rb').read())
image.add_header('Content-ID', '<image1>')
msg.attach(image)

以上是将img图片以附件格式发送,若让其插在正文中,代码改成

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'abc@163.com'
receiver = 'def@163.com'
subject = 'Python email image testing2'
smtpserver = 'smtp.163.com'
username = "abc@163.com"
password = "" # 发送图片插在正文中的邮件
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!','html')
msg.attach(msgText) file1 = r"E:\images\2.jpg"
image = MIMEImage(open(file1, 'rb').read())
image.add_header('Content-ID', '<image1>')
msg.attach(image) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

四、发送附件邮件

import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'abc@163.com'
receiver = 'def@163.com'
subject = 'Python email attachment testing2'
smtpserver = 'smtp.163.com'
username = "abc@163.com"
password = "" # 发送有附件的邮件
msg = MIMEMultipart('related')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' # 构造附件
msgatt = MIMEText(open(r"E:\attachment\ API.docx").read(),'base64','utf-8')
msgatt["Content-Type"] = 'application/octet-stream'
msgatt["Content-Disposition"] = 'attachment;filename="API.docx"'
msg.attach(msgatt) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

五、发送群发邮件

添加多个接收人即可:

receiver = ['def@163.com','lem@hotmail.com','ddd@hotmail.com']

六、发送各种元素都包含的邮件

# -*- coding:utf-8 -*-
import smtplib
import mimetypes
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'abc@163.com'
receiver = 'def@163.com'
subject = 'Python email many testing2'
smtpserver = 'smtp.163.com'
username = "abc@163.com"
password = "" # 构造消息容器
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'lemon<abc@163.com>'
msg['To'] = 'def@163.com' # 构造文件的本身,用html格式:html = """
<html>
<body>
<h1>你好!!欢迎使用html格式</h1>
</body>
</html>""" # 记录以上信息
part2 = MIMEText(html, 'html','utf-8')
msg.attach(part2) # 构造附件
msgatt = MIMEText(open(r"E:\attachment\API.docx").read(),'base64','utf-8')
msgatt["Content-Type"] = 'application/octet-stream'
msgatt["Content-Disposition"] = 'attachment;filename="API.docx"'
msg.attach(msgatt) smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

注意:此处,若将html格式写成

html = """\
Hi!
       How are you?
       Here is the you wanted.
"""

则邮件无法发送到,故一定要写成规范的格式;

七、基于SSL的邮件

用的少,暂未涉及,可参考文档:http://blog.csdn.net/u012063703/article/details/41009955

后续会跟上:http://www.jb51.net/article/49216.htm

python自动化---各类发送邮件方法及其可能的错误的更多相关文章

  1. Python自动化必备发送邮件报告脚本详解

    #!/usr/bin/python3# -*- coding:UTF-8 -*-import smtplib#smtplib库主要用来连接第三方smtp库,用来发邮件from email.mime.t ...

  2. Selenium2+python自动化48-登录方法(参数化)

    前言 登录这个场景在写用例的时候经常会有,我们可以把登录封装成一个方法,然后把账号和密码参数化,这样以后用的登录的时候,只需调用这个方法就行了 一.登录方法 1.把输入账号.输入密码.点击登录按钮三个 ...

  3. appium+python自动化24-滑动方法封装(swipe)

    swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...

  4. Selenium2+python自动化48-登录方法(参数化)【转载】

    前言 登录这个场景在写用例的时候经常会有,我们可以把登录封装成一个方法,然后把账号和密码参数化,这样以后用的登录的时候,只需调用这个方法就行了 一.登录方法 1.把输入账号.输入密码.点击登录按钮三个 ...

  5. appium+python自动化24-滑动方法封装(swipe)【转载】

    swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...

  6. Python自动化之clean方法前端调用clean方法的错误

    obj.non_field_errors.0 源代码: NON_FIELD_ERRORS = '__all__' 如果在前端写 obj.errors.__all__.0直接就会报错 所以经过尝试得知, ...

  7. Selenium2+python自动化59-数据驱动(ddt)

    前言 在设计用例的时候,有些用例只是参数数据的输入不一样,比如登录这个功能,操作过程但是一样的.如果用例重复去写操作过程会增加代码量,对应这种多组数据的测试用例,可以用数据驱动设计模式,一组数据对应一 ...

  8. Selenium2+python自动化52-unittest执行顺序

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  9. Selenium2+python自动化59-数据驱动(ddt)【转载】

    前言 在设计用例的时候,有些用例只是参数数据的输入不一样,比如登录这个功能,操作过程但是一样的.如果用例重复去写操作过程会增加代码量,对应这种多组数据的测试用例,可以用数据驱动设计模式,一组数据对应一 ...

随机推荐

  1. W: 仓库 “http://ppa.launchpad.net/levi-armstrong/qt-libraries-trusty/ubuntu xenial Release” 没有 Release 文件。

    解决办法:将对应的PPA删除掉即可 使用以下命令切换到对应PPA目录: cd /etc/apt/sources.list.dsudo rm levi-armstrong-ubuntu-qt-libra ...

  2. Duilib入门文档提供下载

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Duilib入门文档 基本框架 编写界面xml 响应事件 贴图描述 类html文本描述 动态换肤 Dll插件 资源打包 Duil ...

  3. 《你不知道的javascript》上卷笔记整理(一)

    函数声明和变量声明都会被提升,但函数声明会被提升到普通变量前,而 var foo = function bar(){}; 赋值操作不会被提升. 闭包: 基于词法作用域(作用域是根据名称查找变量的一套规 ...

  4. 一个事件一定时间内只允许点击执行一次 与 vue阻止滚动穿透

    可能我的方法很笨,简单实现来的就是给两个状态,一个状态点击时就发生改变,另外一个给一个定时器延迟改变 篮圈部分,给了两种状态,一个isDisable,一个comeTime 点击事件以后comeTime ...

  5. iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题

    前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...

  6. Js中的onblur和onfocus事件应用介绍

    html页面中,诸如按钮.文本框等可视元素都具有拥有和失去焦点的事件,本文以文本框获得和失去焦点为例简单讲解onfocus和onblur的应用 html页面中,诸如按钮.文本框等可视元素都具有拥有和失 ...

  7. 使用jquery的lazy loader插件实现图片的延迟加载

    当网站上有大量图片要展示的话,如果一次把所有的图片都加载出来的话,这势必会影响网站的加载速度,给用户带来比较差的体验.通过使用jquery的lazy loader插件可以实现图片的延迟加载,当网页比较 ...

  8. java基础之Integer包装类

    Integer类概述: Integer 类在对象中包装了一个基本类型 int 的值 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他 ...

  9. Hyper-V 2016 上安装windows7激活重启后黑屏无法进入系统

    激活重启后就出现下图,无法进入系统 出现此种情况是由于win7的开机引导损坏导致的,具体解决办法如下: 1.设置光盘启动 2.关闭系统重新启动进行修复 启动后按住shift+f10进行修复 输入以下命 ...

  10. 廖雪峰Java11多线程编程-1线程的概念-5中断线程

    1.中断线程: 如果线程需要执行一个长时间任务,就可能需要中断线程.场景:从网络上下载一个100M的文件,用户在下载过程中中断下载任务的执行. 中断线程就是其他线程给该线程发一个信号,该线程收到信号后 ...