python3.4实现邮件发送功能

yipeiwu_com7年前Python基础

本文实例为大家分享了python实现邮件发送功能的具体代码,供大家参考,具体内容如下

import smtplib 
import os 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email import encoders 
user = '*******@qq.com' 
pwd = '*******' 
to = ['******@139.com', '******@qq.com'] 
msg = MIMEMultipart() 
msg['Subject'] = '这里是主题...' 
content1 = MIMEText('这里是正文!', 'plain', 'utf-8') 
msg.attach(content1) 
attfile = 'C:\\Users\\hengli\\Pictures\\CameraMan\\哈哈.doc' 
basename = os.path.basename(attfile) 
fp = open(attfile, 'rb') 
att = MIMEText(fp.read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename)) 
encoders.encode_base64(att) 
msg.attach(att) 
#----------------------------------------------------------- 
s = smtplib.SMTP('smtp.qq.com') 
s.login(user, pwd) 
s.sendmail(user, to, msg.as_string()) 
print('发送成功') 
s.close() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python collections模块使用方法详解

Python collections模块使用方法详解

一、collections模块 1.函数namedtuple (1)作用:tuple类型,是一个可命名的tuple (2)格式:collections(列表名称,列表) (3)̴...

便捷提取python导入包的属性方法

很多时候我们都需要了解下python中导入包的属性方法信息,当然dir 是最便捷的了,不过如果想知道特定的,例如以_ 开头的属性,需要写个筛选,以下是实现筛选的两种方式,主要是练习下yi...

Python scikit-learn 做线性回归的示例代码

Python scikit-learn 做线性回归的示例代码

一、概述 机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得其中各算法理论,叫你喊上一两个著名算法的名字,你也能昂首挺胸脱口而出。当然了,算法之林虽大,但能者...

python开发之tkinter实现图形随鼠标移动的方法

python开发之tkinter实现图形随鼠标移动的方法

本文实例讲述了python开发之tkinter实现图形随鼠标移动的方法。分享给大家供大家参考,具体如下: 做这个东西的时候,灵感源自于一个js效果: 两个眼睛随鼠标移动而移动 运行效果:...

Python Deque 模块使用详解

创建Deque序列: from collections import deque d = deque() Deque提供了类似list的操作方法: d = deque(...