importsmtplib
fromemail.mime.textimportMIMEText
mailto_list=["xxx@","xxx@"]
mail_host=""#设置服务器
mail_user="xxx@"#用户名
mail_pass="xxx"#密码
defsend_mail(to_list,subject,content):#mailto_list:收件人;subject:主题;content:邮件内容
me="Test"+""#这里的Test可以任意设置,收到信后,将按照设置显示
msg=MIMEText(content,_subtype='html',_charset='utf8')#创建一个实例,这里设置为html格式邮件
msg['Subject']=subject#设置主题
msg['From']=me
msg['To']=";".join(to_list)
try:
s=smtplib.SMTP()
s.connect(mail_host,587)#连接smtp服务器
s.ehlo()
s.starttls()
s.login(mail_user,mail_pass)#登陆服务器
s.sendmail(me,to_list,msg.as_string())#发送邮件
s.close()
returnTrue
exceptException,e:
printe
returnFalse
if__name__=='__main__':
ifsend_mail(mailto_list,"hello","测试"):
print"发送成功"
else:
print"发送失败"