代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
实现发送邮件
作者:
恶魔果实
/ 发布于
2015/7/9
/
694
#_*_encoding:utf-8_*_ #script for python3.2 #------------------------------------------------------------------------------- # Name: 发送邮件 # Purpose: # # Author: QiuChangJie # # Created: 10/09/2012 # Copyright: (c) cj.qiu 2012 # Licence: <your licence> #------------------------------------------------------------------------------- import os import smtplib import mimetypes from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.mime.audio import MIMEAudio from email.mime.image import MIMEImage from email.encoders import encode_base64 MAIL_163_USER = "test@163.com" MAIL_163_PWD = "test" MAIL_YEAH_USER = "test@yeah.com" MAIL_YEAH_PWD = "test" MAIL_GOOGLE_HOST = "smtp.gmail.com" MAIL_163_HOST = "smtp.163.com" MAIL_YEAH_HOST = "smtp.yeah.com" RECIPIENT = ["test@dinglicom.com"] ATTACHMENTS = [] class QMail(): def __init__(self, user, pwd, host): self.mail_user = user self.mail_pwd = pwd self.mail_server = smtplib.SMTP() self.mail_server.connect(host) self.mail_server.ehlo() self.mail_server.starttls() self.mail_server.ehlo() self.mail_server.login(self.mail_user, self.mail_pwd) def __del__(self): self.mail_server.close() def send_mail(self, recipient, subject, text, att_files=[]): msg = MIMEMultipart() msg["From"] = self.mail_user msg["Subject"] = subject msg["To"] = ",".join(recipient) msg.attach(MIMEText(text)) if len(att_files) > 0: for file_name in att_files: msg.attach(self.get_attachment(file_name)) self.mail_server.sendmail(self.mail_user, recipient, msg.as_string()) def get_attachment(self, file_name): content_type, encoding = mimetypes.guess_type(file_name) if content_type is None or encoding is not None: content_type = "application/octet-stream" main_type, sub_type = content_type.split('/', 1) file = open(file_name, "rb") if main_type == "text": attachment = MIMEText(file.read()) elif main_type == 'message': attachment = email.message_from_file(file) elif main_type == 'image': attachment = MIMEImage(file.read(), _subType=sub_type) elif main_type == 'audio': attachment = MIMEAudio(file.read(), _subType=sub_type) else: attachment = MIMEBase(main_type, sub_type) attachment.set_payload(file.read()) encode_base64(attachment) file.close() attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_name)) return attachment def test(): mail = QMail("test@163.com", "test", MAIL_163_HOST) mail.send_mail(["test@dinglicom.com"], "sub_test", "text_test", r"G:\WorkSpace\Doing\CMMI文档模板.dot") if __name__ == '__main__': test()
试试其它关键字
发送邮件
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
恶魔果实
贡献的其它代码
(
2
)
.
实现发送邮件
.
超快高效率统计大文件行数
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3