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

python发送邮件

时间:2015-11-23 14:53:09      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:python

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = ‘***@139.com‘

receiver = ‘***@139.com‘

smtpserver = ‘smtp.139.com‘

username = ‘***@139.com‘

password = ‘******‘

smtp = smtplib.SMTP()


mail_body=‘hello, this is the mail content from python and it is sended to two emails !‘

msgText=MIMEText(mail_body)

msgRoot = MIMEMultipart(‘related‘)

msgRoot[‘Subject‘] = ‘hello‘ #邮件主题


msgRoot.attach(msgText)


while 1:#持续尝试发送,直到发送成功

        try:

            smtp.sendmail(sender, receiver, msgRoot.as_string())#发送邮件

            break

        except:

            try:

                smtp.connect(smtpserver)#连接至邮件服务器

                smtp.login(username, password)#登录邮件服务器

            except:

                print "failed to login to smtp server"#登录失败


本文出自 “dba天空” 博客,请务必保留此出处http://9425473.blog.51cto.com/9415473/1715933

python发送邮件

标签:python

原文地址:http://9425473.blog.51cto.com/9415473/1715933

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