Python 发送带附件邮件客户端

参考自:http://blog.csdn.net/wyuan8913/article/details/6917873


想说一下坑了我不少时间的几点:

1.

from email.mime.multipart import MIMEMultipart 

我猜是因为版本的问题,之前使用MIMEMultipart的import不是这么写的。一直报错。

2.

content = MIMEText(text, 'plain','utf-8')

这里的'plain' 在参考链接里写的是'text',然后也一直报错,改成这样就成功了。

3.

password=password.strip('\n')

之前写成password.strip('\n'),发现一直没有去除比较末尾的换行符,还以为是strip失灵了,蛋疼了无数时间之后才改了错。。



#! /usr/local/ActivePython-3.2/bin
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

receiver = '*****@qq.com'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '*****@163.com'
sender=username
with open('passwd.txt') as file:
    password = file.readline()
password=password.strip('\n')

msg=MIMEMultipart('alternative')
msg['Subject']='test message'

text='你好'
content = MIMEText(text, 'plain','utf-8') 
msg.attach(content)

#create the attachment
attfile='buptsnow.jpg' 
att=MIMEText(open(attfile,'rb').read(),'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="buptsnow.jpg"'
msg.attach(att)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值