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

实例一:利用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学习(29)--tkinter图形界面编程2


下一篇:入驻“云栖社区”