一般最好有个smtp娘般点从风衡汉展服务器,比如说你在163注册个邮箱,这样可以用smtplib通过这个邮箱来发送。以下是示例:
#-*-coding:utf8-*-
importsmtplib
importem气哪众组原宣百集剂ail
impor众笔位运川气药tmimetypes
fromemai局低东预题杆什l.MIMEMultipartimportMIMEMultipart
fromemail.mime.textimpo营rtMIMEText
mail_区源配host="smtp.163.com"
mail_user="yourusername"
mail_pass="yourpassword"
mail_postfix="mail.163.com"
defsendmail(to_list,思省攻下跟担sub,con):
"""发送邮件
"""
#tra美是少nslation
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
m哥同赶投编右sg=MIMEMultipart('related')
msg['Subject'造号]=email.Header.Heade多欢思由调还都乡古间r(sub,'utf-8')
msg['From虽室委油知例岩']=me
msg['To']=";".join(to班_list)
msg.preamble='Thisisamulti-partmessageinMIMEformat.'
msgAlternative=MIMEMultipart('alt被显ernative')
msgText=MIME优深圆省Text(con,'p造批木危小语坏担律始lain','utf-8')
msgAlternative.attach(msgText)
msg.attach(msgAlternative)
try:
s=smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.quit()
exceptException,e:
returnFalse
returnTrue
if__name__=='__main__':
ifsendmail(['test@test.com'],"测试","测试"):
战print"Success!"
else:
print"Fail!"
如果要不经过邮件系统直接发,通常会被当作垃圾邮件扔了,所以还是这样吧。