5

我很难确定文本电子邮件的正文是否是 base64 编码的。如果是,则使用这行代码;使用 jython 2.2.1

dirty=base64.decodebytes(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.decodebytes(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.decodebytes(dirty)
            else:
                return "cannot obtain the body of the email"
        else:
            return "cannot obtain the body of the email"
        return dirty
    except:
        raise

好的,这段代码现在可以工作了!谢谢大家

4

2 回答 2

6

尝试:

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

这是一个标题,因此您将无法看到它的身体。您应该能够在同一地点找到主题。

于 2008-11-28T02:44:04.760 回答
1

它是一个标头,但您必须首先从消息中获取有效负载。

这将是:

header = msg.get_payload()[0]
header['Content-Transfer-Encoding']

我正在使用 Python 3

于 2017-05-25T18:00:07.227 回答