python连接腾讯企业邮箱发送html邮件

作者: shaneZhang 分类: 互联网技术 发布时间: 2019-01-05 18:52

在工作中难免用到一些自动化的邮件配置,这里用python简单写了一个连接腾讯企业邮箱的脚本邮件代码。备忘下来。

# coding=utf-8

import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart


# 发送邮件
def send_Email():
    # 邮件接受者
    mail_receiver = ['xxxx@xxx.com']
    # 根据不同邮箱配置 host,user,和pwd
    mail_host = 'smtp.exmail.qq.com:465'
    mail_user = ''
    mail_pwd = ''
    mail_to = ','.join(mail_receiver)
    msg = MIMEMultipart()
    environsString = '<h3>Hello This is the text mail.</h3><p>'
    message = environsString
    body = MIMEText(message, _subtype='html', _charset='utf-8')
    msg.attach(body)
    msg['To'] = mail_to
    msg['from'] = mail_user
    msg['subject'] = 'EmailTest'

    try:
        # s = smtplib.SMTP()
        s = smtplib.SMTP_SSL()
        s.connect(mail_host)
        # s.starttls()
        s.login(mail_user, mail_pwd)
        s.sendmail(mail_user, mail_receiver, msg.as_string())
        s.close()
        print 'success'
    except Exception, e:
        print e


if __name__ == "__main__":
    send_Email()

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注