1. python发送邮件代码如下
# -*-coding:utf8-*-
"""
=========================================
author: Lujier time: 2019/7/1
E-mail: 2327994109@qq.com
==========================================
"""
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发送带附件邮件需要下边两个库
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
"""
该模块的邮件发送包含附件
"""
# 创建一个stmp对象
s = smtplib.SMTP()
# 连接到SMTP服务器
host = "smtp.163.com" # 注意163邮箱的smtp用的是25端口,qq用的是465端口
s.connect(host, 25)
# 登录SMTP服务器
mail_account = '15934815829@163.com' # 发件邮箱
mail_pwd = '*******' # 授权码
s.login(user=mail_account, password=mail_pwd)
# 构建一封邮件
mail_content = MIMEMultipart()
# 构建邮件内容
Subject = '2019/07/01邮件发送' # 邮件主题
mail_content['Subject'] = Header(Subject, 'utf8')
From = mail_account
To = '450615105@qq.com'
mail_content['From'] = From # 发件人
mail_content['To'] = To # 收件人
content = MIMEText('测试邮件是否发送成功') # 邮件正文
mail_content.attach(content)
# 构建附件
# 1. 附件路径
filepath = r"""D:\APP_Workplaces\AutoTest_Learn\requests_tests02\request20190624\reports\20190628093654TestReport.html"""
fileObj = MIMEApplication(open(filepath, 'rb').read(), _subtype=None)
fileObj.add_header('content-disposition', 'attachment', filename='report.html')
# 邮件添加附件
mail_content.attach(fileObj)
# 发送邮件
s.sendmail(from_addr=From, to_addrs=To, msg=mail_content.as_string())
2. 结果运行报如下截图错误:
Traceback (most recent call last):
File "D:/App_Workplaces/AutoTest_Learn/AutoTest_Learn/requeststest_02/homework/web_service_20190701/send_email_file.py", line 49, in <module>
fileObj = MIMEApplication(open(filepath, 'rb').read(), _subtype=None)
File "D:\APP_Installs\python3.7\lib\email\mime\application.py", line 33, in __init__
raise TypeError('Invalid application MIME subtype')
TypeError: Invalid application MIME subtype
3. 问题分析:
通过报错信息我们进入源码application模块中,并定位在application模块 __init__中,init中代码如下:
def __init__(self, _data, _subtype='octet-stream',
_encoder=encoders.encode_base64, *, policy=None, **_params):
"""Create an application/* type MIME document.
_data is a string containing the raw application data.
_subtype is the MIME content type subtype, defaulting to
'octet-stream'.
_encoder is a function which will perform the actual encoding for
transport of the application data, defaulting to base64 encoding.
Any additional keyword arguments are passed to the base class
constructor, which turns them into parameters on the Content-Type
header.
"""
if _subtype is None:
raise TypeError('Invalid application MIME subtype')
MIMENonMultipart.__init__(self, 'application', _subtype, policy=policy,
**_params)
self.set_payload(_data)
_encoder(self)
对比异常信息,TypeError: Invalid application MIME subtype,那我们就知道,是因为_subtype is None,所以抛出异常,未走
MIMENonMultipart.__init__(self, 'application', _subtype, policy=policy , **_params)
这时候,我们就知道了原因了: 是我们在创建 MIMEApplication 对象时,参数 _subtype is None
4. 解决方法:
在我们自己发送邮件的代码中,修改一行代码:
fileObj = MIMEApplication(open(filepath, 'rb').read(), _subtype=False)
当然: _subtype也可以是其他,不一定非要是False
这时候,邮件就可以发送成功了