# -*- coding:utf-8 -*-
#!/usr/bin/python
import sys
reload(sys) # reload 才能调用 setdefaultencoding 方法
sys.setdefaultencoding(‘utf-8‘) # 设置 ‘utf-8‘
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#第三方服务smtp
mail_host=‘smtp.yeah.net‘
mail_user=‘xxx‘
mail_pass=‘xxx‘
mail_postfix= ‘yeah.net‘
receivers=‘[email protected]‘
def send_mail():
me = mail_user+"<"+mail_user+"@"+mail_postfix+">" #邮件发送方
subject = "Python SMTP 邮件测试test"
message = MIMEText(‘Python 邮件发送测试...‘, ‘plain‘, ‘utf-8‘) #邮件内容
message[‘subject‘] = subject #邮件标题
message[‘from‘] = me #发送方
message[‘to‘] = receivers #接收方
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host) #连接smtp
smtpObj.login(mail_user,mail_pass) #登录
smtpObj.sendmail(me,receivers, message.as_string()) #获取参数发送邮件
smtpObj.close() #关闭
print "邮件发送成功"
except smtplib.SMTPException,e:
print "ERROR,无法发送邮件"
print e
if __name__=="__main__":
send_mail()
原文地址:http://blog.51cto.com/yzg784534398/2063656