python3.4实现邮件发送功能

yipeiwu_com6年前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 opencv 二值化 计算白色像素点的实例

贴部分代码 #! /usr/bin/env python # -*- coding: utf-8 -*- import cv2 import numpy as np from PIL...

Python3显示当前时间、计算时间差及时间加减法示例代码

Python3显示当前时间、计算时间差及时间加减法示例代码

摘要 在使用Python写程序时,经常需要输出系统的当前时间以及计算两个时间之间的差值,或者将当前时间加减一定时间(天数、小时、分钟、秒)来得到新的时间,这篇文章就系统的对这些进行总结...

Python isinstance判断对象类型

复制代码 代码如下:if (typeof(objA) == typeof(String)) { //TODO } 在Python中只需要使用内置的函数isinstance,使用起来非常简...

python requests post多层字典的方法

pyhton requests模块post方法传参为多层字典时,转换错误, 如,表单传参 { “a”:1, “b”:{ “A”:2, “B”:3 } } post请求...

python将控制台输出保存至文件的方法

很多时候在Linux系统下运行python程序时,控制台会输出一些有用的信息。为了方便保存这些信息,有时需要对这些信息进行保存。这里介绍几种将控制台输出保存到文件中的方式: 1 重定向标...