python3 发送任意文件邮件的实例

yipeiwu_com6年前Python基础

实例如下所示:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''):
 msg = email.mime.multipart.MIMEMultipart()
 msg['from'] = sendAddr
 msg['to'] = recipientAddrs
 msg['subject'] = subject
 content = content
 txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
 msg.attach(txt)


 # 添加附件,传送D:/mydev/yasuo.rar文件
 part = MIMEApplication(open('D:/mydev/6.rar','rb').read())
 part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar")
 msg.attach(part)

 smtp = smtplib.SMTP()
 smtp.connect(smtpHost, '25')
 smtp.login(sendAddr, password)
 smtp.sendmail(sendAddr, recipientAddrs, str(msg))
 print("发送成功!")
 smtp.quit()

try:

subject = 'Python 测试邮件'
content = '这是一封来自 Python 编写的测试邮件。'
send_email('smtp.163.com', '18310161797@163.com', '邮箱密码', '526189064@qq.com', subject, content)
except Exception as err:
print(err)

以上这篇python3 发送任意文件邮件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python Collatz序列实现过程解析

编写一个名为 collatz()的函数,它有一个名为 number 的参数。如果参数是偶数,那么 collatz()就打印出 number // 2, 并返回该值。如果 number 是...

python删除不需要的python文件方法

最近在看廖老师的python教程,在看到关于文件的操作时,廖老师的其中一段关于查找电脑里的python文件,突然想把之前写的python代码给删除了,因为这是第二次看教程了。 imp...

Python基于回溯法子集树模板实现8皇后问题

Python基于回溯法子集树模板实现8皇后问题

本文实例讲述了Python基于回溯法子集树模板实现8皇后问题。分享给大家供大家参考,具体如下: 问题 8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、...

详解python中的index函数用法

1.函数的创建 def fun():        #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun()...

利用Python校准本地时间的方法教程

利用Python校准本地时间的方法教程

1. 概念 1.1 基本概念 时间,对于我们来说很重要,什么时候做什么?什么时候发生什么?没有时间的概念,生活就乱了。 在日常的运维当中,我们更关注告警的时间:什么时候发生、什么事故...