发送邮件

不羁岁月 提交于 2019-12-11 13:44:14
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: 无法发送邮件")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!