python SMTP
2017-11-15 14:06 woodzcl 阅读(396) 评论(0) 编辑 收藏 举报一。一开始,相信SMTP服务,所以在本机安装了一个
apt-get install sendmail
apt-get install sendmail-cf
apt-get install squirrelmail
apt-get install spamassassin
apt-get install mailman
apt-get install mailutils
apt-get install sharutils
验证SMTP完成
ps aux | grep sendmail
root 5980 0.0 0.2 102796 2676 ? Ss 18:34 0:00 sendmail: MTA: accepting connections
root 14091 0.0 0.0 15948 944 pts/25 S+ 21:46 0:00 grep --color=auto sendmail
//test.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'from@xxx.com'
receivers = ['791398105@qq.com']
message = MIMEText('mail send test...', 'plain', 'utf-8')
message['Subject'] = Header('Python SMTP mailtest', 'utf-8')
message['From'] = Header('my', 'utf-8')
message['To'] = Header('test', 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message.as_string())
print 'succeed in sending'
except smtplib.SMTPException:
print "Error: can not send mail"
试试效果
python test.py
succeed in sending
自我感觉良好,但是QQ邮箱里全空空也!!!!??????????????
what the fuck!!!
二。于是乎,本机python通过本机SMTP发送不成功
试试SMTP的mail命令吧,
echo "content" | mail -s subject 791398105@qq.com
成功了!,不过邮件在垃圾箱里
如此看来,是python 使用了未更新的SMTP功能,而mail已经是最新的了
那么怎么办,于是网上转转吧!!!
什么,最好连你的目的地址的SMTP服务器
OK,do it!
&获得QQ之SMTP的邮箱授权码
发送短信,下边的内容至指定号码
选择我已发送,收到授权码
改变时效-无限期
最后,记得保存设置哦
&试试这个QQ的SMTP吧
//test1.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host="smtp.qq.com"
mail_user="791398105@qq.com"
mail_pass="***************" #your authorized code
sender='791398105@qq.com'
receivers=['791398105@qq.com']
message=MIMEText('Test sending mail for python','plain','utf-8')
message['From']=Header("my",'utf-8')
message['To']= Header("you",'utf-8')
subject='Test mail, please not open it'
message['Subject']=Header(subject,'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(mail_host,465)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print "Succeed in sending mail"
except smtplib.SMTPException,e:
print e
//result
你看我收到了,而且还是在正常的收件箱里哦
Finally:
有时候,你想让谁干事,最好还是直接告诉他,别拐弯抹角的,也许效果更好呢,对吧,你说呢?