自动收取邮件的python代码出错,求高手指教

hollin1988 2012-06-15 11:14:25
我想用邮件写个自动收取邮件,然后自动下到本机的python代码,但是在写入文件的时候,老是自动报错,我要把报错的地方注释掉,就不能写内容。求教高手

# -*- coding: cp936 -*-
import poplib
import cStringIO
import email
import base64
import os

pop3_server = 'pop.163.com'
user_name = 'XXXX@163.com'
password = 'XXXX'
#POP3取信
M = poplib.POP3(pop3_server)
M.user(user_name)
M.pass_(password)
#打印有多少封信
numMessages = len(M.list()[1])
print ('num of messages', numMessages)

for i in range(numMessages):
m = M.retr(i+1)
buf = cStringIO.StringIO()
for j in m[1]:
print >> buf, j
buf.seek(0)
#解析信件内容
msg = email.message_from_file(buf)
for part in msg.walk():
contenttype = part.get_content_type()
filename = part.get_filename()
if contenttype == 'application/octet-stream':
if filename == None:
continue
# 保存附件
f = open( "mail%d_%s.attach" % (i+1,filename), 'wb')
f.write(base64.decodestring(part.get_payload()))
f.close()
elif contenttype == 'text/plain':
# 保存正文
f = open( "mail%d.eml " % (i+1), 'wb')
f.write(base64.decodestring(part.get_payload()))
f.close()



出错信息是:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\任务\邮件库\getEmail.py", line 40, in <module>
f.write(base64.decodestring(part.get_payload()))
File "C:\Python26\lib\base64.py", line 321, in decodestring
return binascii.a2b_base64(s)
Error: Incorrect padding
...全文
282 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-06-15
  • 打赏
  • 举报
回复
这个错误说明你要解码的字符串中有错误的填充字符、或者非字母字符,
http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding
bugs2k 2012-06-15
  • 打赏
  • 举报
回复
def processMsg(entire_msg):
body = ''
msg = email.message_from_string(entire_msg)
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == 'text/plain':
body = part.get_payload()
break
else:
body = msg.get_payload(decode=True)
else:
body = msg.get_payload(decode=True)
return body
hollin1988 2012-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

f.write(base64.decodestring(part.get_payload())) =》

f.write(base64.b64decode(part.get_payload())) 试试?
[/Quote]

试了报这个错误:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\任务\邮件库\getEmail.py", line 40, in <module>
f.write(base64.b64decode(part.get_payload()))
File "C:\Python26\lib\base64.py", line 76, in b64decode
raise TypeError(msg)
TypeError: Incorrect padding
Gloveing 2012-06-15
  • 打赏
  • 举报
回复
f.write(base64.decodestring(part.get_payload())) =》

f.write(base64.b64decode(part.get_payload())) 试试?
Gloveing 2012-06-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:

这个错误说明你要解码的字符串中有错误的填充字符、或者非字母字符,
http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding



那请问怎么解决呢?
[/Quote]
链接里有解决思路:
http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding
hollin1988 2012-06-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

这个错误说明你要解码的字符串中有错误的填充字符、或者非字母字符,
http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding
[/Quote]


那请问怎么解决呢?

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧