python脚本发送普通邮件

#!/usr/bin/python
#coding=UTF-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host = "smtp.163.com"
mail_user = "网易账号@163.com"
mail_pass = "SMTP服务密码"
sender = '网易账号@163.com'
receivers = ['QQ号@qq.com']

#三个参数:第一个为内容第二个是文本格式第三个是utf-8编码
message = MIMEText('你奈我何', 'plain', 'utf-8')
message['From'] = '网易账号@163.com'
message['To'] =  'QQ号@qq.com'

subject = "凯"
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.login(mail_user,mail_pass)
#    i = 1
#    while(i < 10):
    smtpObj.sendmail(sender, receivers, message.as_string())
#       i = i + 1
    print "邮件发送成功"
except smtplib.SMTPException,e:
    print "Error: 无法发送邮件"
    print e;

1.由于没有下载sendmail,可以使用其他邮件服务商的 SMTP 访问(QQ、网易、Google等)。
2.如果有报错,可以上报错指定的网站查看错误信息代号。我遇到过内容问题报错,验证不通过,发送邮件数量过大等错误
3.还有一点要注意:message[‘From’] 和message[‘To’]要与sender 和receivers一致

你可能感兴趣的:(python)