Javax 邮件 api:获取 message.getdescription()= null

Javax mail api: getting message.getdescription()= null

我通过 imap 阅读邮件,并且能够获取邮件、邮件主题、发送日期和发件人地址。但是消息描述是null。我用的是 Javax mail api 1.4。

从 MimeMesage documentation,getDescription() 方法用于获取消息的 "Content-Description" header 字段。

如果要提取 body,可以在消息 object.

上使用 MimeMessageParser
final MimeMessageParser parser = new MimeMessageParser(message);
String body = null;
try {
    parser.parse();
} catch (Exception e) {
    LOG.error("Exception during parsing the message body: {}", e);
}
if (parser.hasPlainContent()) {
    body = parser.getPlainContent();
} else if (parser.hasHtmlContent()) {
    body = parser.getHtmlContent();
}

MimeMessage::getDescription()javadoc 说:

Returns the "Content-Description" header field of this Message. This typically associates some descriptive information with this part. Returns null if this field is unavailable or its value is absent.

因此,如果 getDescription() 返回 null,这意味着您尝试阅读的 MIME 邮件没有该字段,或者该字段为空。

缺少“Content-Description”是合法的,因为 RFC 2045 指出:

This header field is always optional.


简而言之,您的应用程序应设计为应对 getDescription() 返回 null


您发表评论:

message.getDescription() was supposed to give me body of the mail. which im getting null.

那不是 getDescription() 所做的。如果你想要邮件的正文,使用getContent()getContentAsStream()