126邮箱发送邮件脚本

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from email.header import Header
from smtplib import SMTP_SSL

def send_mail():
    try:
        host_server = 'smtp.126.com'
        # sender_qq为发件人的邮126箱
        sender_qq = '********@126.com'
        # pwd为126邮箱的授权码
        pwd = '********'  
        # 发件人的邮箱
        sender_qq_mail = '*********@126.com'
        # 收件人邮箱
        receiver = '*********@qq.com'

        # 邮件的正文内容
        mail_content = 'python test email content'
        # 邮件标题
        mail_title = 'test email'

        # ssl登录
        smtp = SMTP_SSL(host_server)
        # set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
        smtp.set_debuglevel(1)
        smtp.ehlo(host_server)
        smtp.login(sender_qq, pwd)

        msg = MIMEText(mail_content, "plain", 'utf-8')
        msg["Subject"] = Header(mail_title, 'utf-8')
        msg["From"] = sender_qq_mail
        msg["To"] = receiver
        smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
        smtp.quit()
        return True
    except Exception as e:
        print(e)
        return False
if __name__ == '__main__':
    if send_mail():
        print("发送成功")
    else:
        print("发送失败")

猜你喜欢

转载自blog.csdn.net/bigcarp/article/details/143102088