python连接腾讯企业邮箱发送html邮件
在工作中难免用到一些自动化的邮件配置,这里用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()