如果要发送其他类型的,如果PDF、doc、xls、MP3格式的,我们都可以通过MIMEApplication来完成,MIMEApplication默认子类型是application/octet-stream,而application/octet-stream表明这是个二进制文件,但愿接收方知道怎么处理!!!,然后客户端收到这个声明后会根据文件扩展名来猜测。
版一:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header

# 第三方 SMTP 服务
# 设置服务器
mail_host = "smtp.qq.com"

# 用户名
mail_user = "1320685524@qq.com"

#获取授权码
mail_pass="mpaocydzpzfjidge"

# 发件人账号
sender = \'1320685524@qq.com\'

# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
receivers = [\'1320685524@qq.com\']

# 创建一个带附件的实例
message = MIMEMultipart()

# 发件人
message[\'From\'] = Header("我是发件人", \'utf-8\')

# 收件人
message[\'To\'] = Header("我是收件人", \'utf-8\')

subject = \'Python发送带附件的邮件示例\'
message[\'Subject\'] = Header(subject, \'utf-8\')

# 邮件正文内容
send_content = \'hi man,你收到附件了吗?\'
content_obj = MIMEText(send_content, \'plain\', \'utf-8\')
message.attach(content_obj)

# 构造附件1,发送当前目录下的 t1.txt 文件
part1 = MIMEText(open(\'t1.txt\', \'rb\').read(), \'base64\', \'utf-8\')
part1.add_header(\'Content-Disposition\', \'attachment\', filename=\'t1.txt\')
message.attach(part1)

# 构造附件2,发送当前目录下的 t6.mp3 文件
part2 = MIMEText(open(\'t6.mp3\', \'rb\').read(), \'base64\', \'utf-8\')
part2.add_header(\'Content-Disposition\', \'attachment\', filename=\'bg.mp3\')
message.attach(part2)

# 构造附件3,发送当前目录下的 t3.xls 文件
part3 = MIMEText(open(\'t3.xls\', \'rb\').read(), \'base64\', \'utf-8\')
part3.add_header(\'Content-Disposition\', \'attachment\', filename=\'t3.xls\')
message.attach(part3)

# 构造附件4,发送当前目录下的 t4.doc 文件
part4 = MIMEText(open(\'t4.doc\', \'rb\').read(), \'base64\', \'utf-8\')
part4.add_header(\'Content-Disposition\', \'attachment\', filename=\'t4.doc\')
message.attach(part4)

# 构造附件5,发送当前目录下的 t5.pdf 文件
part5 = MIMEText(open(\'t5.pdf\', \'rb\').read(), \'base64\', \'utf-8\')
part5.add_header(\'Content-Disposition\', \'attachment\', filename=\'t5.pdf\')
message.attach(part5)

# 构造附件6,发送当前目录下的 t7.jpg 文件
part6 = MIMEText(open(\'t7.jpg\', \'rb\').read(), \'base64\', \'utf-8\')
part6.add_header(\'Content-Disposition\', \'attachment\', filename=\'img.jpg\')
message.attach(part6)

try:
smtpObj = smtplib.SMTP()
# 25 为 SMTP 端口号
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")

版二:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header

# 第三方 SMTP 服务
# 设置服务器
mail_host = "smtp.qq.com"

# 用户名
mail_user = "1320685524@qq.com"

#获取授权码
mail_pass="mpaocydzpzfjidge"

# 发件人账号
sender = \'1320685524@qq.com\'

# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
receivers = [\'1320685524@qq.com\']

# 创建一个带附件的实例
message = MIMEMultipart()

# 发件人
message[\'From\'] = Header("我是发件人", \'utf-8\')

# 收件人
message[\'To\'] = Header("我是收件人", \'utf-8\')

subject = \'Python发送带附件的邮件示例\'
message[\'Subject\'] = Header(subject, \'utf-8\')

# 邮件正文内容
send_content = \'hi man,你收到附件了吗?\'
content_obj = MIMEText(send_content, \'plain\', \'utf-8\')
message.attach(content_obj)

# 构造附件1,发送当前目录下的 t1.txt 文件
part1 = MIMEApplication(open(\'t1.txt\', \'rb\').read())
part1.add_header(\'Content-Disposition\', \'attachment\', filename=\'t1.txt\')
message.attach(part1)

# 构造附件2,发送当前目录下的 t6.mp3 文件
part2 = MIMEApplication(open(\'t6.mp3\', \'rb\').read())
part2.add_header(\'Content-Disposition\', \'attachment\', filename=\'bg.mp3\')
message.attach(part2)

# 构造附件3,发送当前目录下的 t3.xls 文件
part3 = MIMEApplication(open(\'t3.xls\', \'rb\').read())
part3.add_header(\'Content-Disposition\', \'attachment\', filename=\'t3.xls\')
message.attach(part3)

# 构造附件4,发送当前目录下的 t4.doc 文件
part4 = MIMEApplication(open(\'t4.doc\', \'rb\').read())
part4.add_header(\'Content-Disposition\', \'attachment\', filename=\'t4.doc\')
message.attach(part4)

# 构造附件5,发送当前目录下的 t5.pdf 文件
part5 = MIMEApplication(open(\'t5.pdf\', \'rb\').read())
part5.add_header(\'Content-Disposition\', \'attachment\', filename=\'t5.pdf\')
message.attach(part5)

# 构造附件6,发送当前目录下的 t7.jpg 文件
part6 = MIMEApplication(open(\'t7.jpg\', \'rb\').read())
part6.add_header(\'Content-Disposition\', \'attachment\', filename=\'img.jpg\')
message.attach(part6)

try:
smtpObj = smtplib.SMTP()
# 25 为 SMTP 端口号
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")

版权声明:本文为zhang-da原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhang-da/p/12231758.html