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

python3使用stmplib发送邮件

时间:2017-08-03 15:39:54      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:import   表示   tmp   stmp   mail   邮件服务器   except   orm   lib   

 

代码如下:

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr

# 第三方 SMTP 服务
mail_host = "smtp.exmail.qq.com"
mail_port = 465  # 也可能是25,看情况。SMTPS的话一般是465
mail_user = "a@a.com"
mail_pass = "aaa"   # 密码或邮箱授权码

mail_sender = a@a.com
mail_receivers = [a@a.com]

subject = 邮件主题
message = MIMEText(邮件内容, plain, utf-8)  # plain表示邮件类型
message[From] = formataddr(["发件人别称", "a@a.com"])
message[To] = formataddr(["收件人别称", "a@a.com"])
message[Subject] = Header(subject, utf-8)


def sendmail(host, port, sender, password, recvivers, meassage):
    try:
        mail_obj = smtplib.SMTP_SSL()  # 邮件服务器使用 SMTPS 的时候用 SMTP_SSL
        mail_obj.connect(host, port)
        mail_obj.login(sender, password)
        mail_obj.sendmail(sender, recvivers, meassage.as_string())
        mail_obj.quit()
        print("Send sucessful")
        return True
    except Exception as e:
        err = "Send Failed for reason:\n {}".format(e)
        print(err)
        return False


sendmail(mail_host, mail_port, mail_sender, mail_pass, mail_receivers, message)

 

python3使用stmplib发送邮件

标签:import   表示   tmp   stmp   mail   邮件服务器   except   orm   lib   

原文地址:http://www.cnblogs.com/resn/p/7279906.html

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