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"#登录失败


你可能感兴趣的:(python)