2008-11-28 71 views
3

我很难确定文本电子邮件正文是否以base64编码。 如果它然后使用这一行代码; 利用Jython的2.2.1如何确定电子邮件是否是Base64编码的?

dirty=base64.decodestring(dirty) 

其他的继续正常。

这是我的atm代码。什么行代码可以让我从邮件中提取这样的:

“内容传输编码:的base64”

import email, email.Message 
import base64 

def _get_email_body(self): 
    try: 
     parts=self._email.get_payload() 
     check=parts[0].get_content_type() 
     if check=="text/plain": 
      part=parts[0].get_payload() 
      enc = part[0]['Content-Transfer-Encoding'] 
      if enc == "base64": 
       dirty=base64.decodestring(dirty) 
     elif check=="multipart/alternative": 
      part=parts[0].get_payload() 
      enc = part[0]['Content-Transfer-Encoding'] 
      if part[0].get_content_type()=="text/plain": 
       dirty=part[0].get_payload() 
       if enc == "base64": 
        dirty=base64.decodestring(dirty) 
      else: 
       return "cannot obtain the body of the email" 
     else: 
      return "cannot obtain the body of the email" 
     return dirty 
    except: 
     raise 

OKAY此代码的工作吧!感谢所有

+0

如果你能告诉我们你正在使用什么MIME库对象,它可以帮助人们回答你。你在使用Python库还是Java对象? – 2008-11-28 02:53:24

+0

好的谢谢你的提醒,通常对此很好! – Setori 2008-11-28 03:32:41

回答

5

尝试:

enc = msg['Content-Transfer-Encoding'] 

这是一个头,所以你将无法得到它看着身体。你应该能够在同一个地方找到主题。

0

它是一个标题,但你必须先从消息中获得有效载荷。

这将是:

头= msg.get_payload()[0]

页眉[ '内容传输编码']

我使用Python 3

相关问题