#coding=utf-8
\'\'\'
Created on 2017-7-5
@auther:Qigege
Project:发送邮箱测试
\'\'\'
import smtplib
from email.mime.text import MIMEText
#SMTP服务器
mail_host=\'smtp.163.com\'
mail_user=\'******@163.com\'
#网易邮箱为网页版,需要用到客户端密码,进入网页版邮箱中设置授权码,即客户端密码
mail_password=\'******\'
#发件人邮箱
sender=\'******@163.com\'
#接收人邮箱
receiver=[\'******@qq.com\',\'******@qq.com\']
content=u\'邮箱测试......\' #内容
title=\'Python SMTP Mail Test\' #主题
message=MIMEText(content,\'plain\',\'utf-8\')
#邮箱发送者地址
#格式化字符串format(),{}==%,例:‘{1},{0},{1}’.format(\'abc\',12)-->\'12,abc,12\'
message[\'From\']=\'{}\'.format(sender)
#邮件接受者地址,字符串列表[‘接受地址1’,‘接受地址2’,......]或‘接受地址’
message[\'To\']=\',\'.join(receiver) #type(message[\'To\'])为str
message[\'Subject\']=title
try:
#启用SSL,端口一般是465
smtpObj=smtplib.SMTP_SSL(mail_host,465)
#登录验证
smtpObj.login(mail_user,mail_password)
#发送
smtpObj.sendmail(sender,receiver,message.as_string())
#as_string()将MIMEText或MIMEMultipart对象转换为str
print \'mail has been send successfully.\'
except smtplib.SMTPException as e:
print e
相关文章: