发送邮件测试

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.header import Header
 4 
 5 mail_host = "192.168.1.65"  # 设置服务器
 6 mail_user = "admin@leejay.com"  # 用户名
 7 mail_pass = "redhat"  # 口令
 8 receivers = ['cm1@leejay.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
 9 rcpt_to = receivers[0]
10 i = 0
11 while i <= 3:
12     i += 1
13     message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')  #正文
14     message['From'] = Header(mail_user, 'utf-8')  #正文里发件人信息
15     message['To'] = Header(rcpt_to, 'utf-8')  #信头里的收件人
16 
17     subject = 'Python SMTP 邮件测试' + str(i) #标题
18     message['Subject'] = Header(subject, 'utf-8')
19     try:
20         smtpObj = smtplib.SMTP()
21         smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
22         smtpObj.login(mail_user, mail_pass)
23         smtpObj.sendmail(mail_user, receivers, message.as_string())
24         print("邮件发送成功")
25     except smtplib.SMTPException:
26         print("Error: 无法发送邮件")

 

posted @ 2021-01-15 14:11  leejay_python  阅读(708)  评论(0编辑  收藏  举报