发邮件,现在还有不带附件的吗?
开个玩笑,你要带,就得如此下边这样办
//test.py
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
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=MIMEMultipart()
message['From']=Header("my",'utf-8')
message['To']= Header("you",'utf-8')
message.attach(MIMEText('Test mail attached for Python','plain','utf-8'))
subject='Test mail, please not open it'
message['Subject']=Header(subject,'utf-8')
att1=MIMEText(open('f.txt', 'rb').read(), 'base64','utf-8')
att1["Content-Type"]='application/octet-stream'
att1["Content-Disposition"]='attachment;filename="f.txt"'
message.attach(att1)
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
#python test.py
Succeed in sending mail
Finally:
有时候你是把复杂事务的应用也想的复杂了,其实,应用还是蛮简单啊