码迷,mamicode.com
首页 > 编程语言 > 详细

python实现邮件发送完整代码(带附件发送方式)

时间:2014-10-11 21:37:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   art   on   log   代码   ad   

实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码)

__author__ = ‘Administrator‘
#coding=gb2312

from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib, datetime


def SendMailAttach():

msg = MIMEMultipart()
att = MIMEText(open(‘测试.xlsx‘, ‘rb‘).read(), ‘base64‘, ‘gb2312‘)
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="测试.xlsx"‘
msg.attach(att)

msg[‘to‘] = ‘gz2015@aebell.com‘
msg[‘from‘] = ‘gz2015@aebell.com‘
msg[‘CC‘]=‘gz2015@aebell.com‘
msg[‘subject‘] = Header(‘冒烟测试结果 (‘ + str(datetime.date.today()) + ‘)‘,‘gb2312‘)

body = "Python test mail"
msg.attach(MIMEText(body, ‘plain‘))

server = smtplib.SMTP(‘58.62.220.52‘,25)
server.login("用户名","密码")
msg_text=msg.as_string()
server.sendmail(msg[‘from‘], msg[‘to‘],msg_text)
server.close

 

 

实例二:利用SMTP实现简单的邮件发送。(不带附件发送)

__author__ = ‘Administrator‘
#coding=utf-8
import smtplib
msg=["From:gz2015@aebell.com",‘To:gz2015@aebell.com‘,"Subject:Test Demo"]
msgBody="Hello world"
smtp=smtplib.SMTP()
smtp.connect("58.62.220.52",25)
smtp.login("用户名","密码")
smtp.sendmail("gz2015@aebell.com","gz2015@aebell.com","\r\n\r\n".join(("\r\n".join(msg),msgBody)))
print "邮件发送成功!"
smtp.quit()

python实现邮件发送完整代码(带附件发送方式)

标签:io   os   ar   sp   art   on   log   代码   ad   

原文地址:http://www.cnblogs.com/jinjiangongzuoshi/p/4019632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!