使用python语言发送邮件smtp

使用python语言发送邮件smtp,详细见下面脚本

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

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

# 第三方SMTP服务
mail_host = 'smtp.126.com'
mail_user = '[email protected]'
mail_pass = '12345678'

sender = '[email protected]'
receivers = ['[email protected]']

# 三个参数: 第一个为文本内容,第二个plain设置文本格式 ,第三个utf-8设置编码
message = MIMEText('Python 邮件发送测试5。。。', 'plain', 'utf-8')
message['From'] = '[email protected]'   #发送者
message['To'] = '[email protected]'   #接收者

subject = 'Python 测试邮件5'
message['Subject'] = Header(subject, 'utf-8')

smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"

注意上面的接收者可以随便填写,可以是数字字符
同时126邮箱要在设置里开通 邮箱授权密码

猜你喜欢

转载自blog.51cto.com/weiruoyu/2379506