发送邮件

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

mail_host = "192.168.213.187"  # 设置服务器
mail_user = "admin@leejay.vip"  # 用户名
mail_pass = "1"  # 口令
receivers = ['cm3@leejay.vip']  # 接收邮件
rcpt_to = receivers[0]
i = 0
while i <= 200:
    i += 1
    message = MIMEText('Python 邮件发送测试%s...' %i, 'plain', 'utf-8')  #正文
    message['From'] = Header(mail_user, 'utf-8')  #正文里发件人信息
    message['To'] = Header(rcpt_to, 'utf-8')  #正文里的收件人

    subject = 'Python SMTP 邮件测试' + str(i) #标题
    message['Subject'] = Header(subject, 'utf-8')
    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(mail_user, receivers, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
posted @ 2019-12-11 13:28  leejay_python  阅读(153)  评论(0编辑  收藏  举报