使用 JavaMail 读取电子邮件时,MimeMultipart 计数为零

Posted

技术标签:

【中文标题】使用 JavaMail 读取电子邮件时,MimeMultipart 计数为零【英文标题】:MimeMultipart count is zero when an email is read using JavaMail 【发布时间】:2017-06-04 05:22:49 【问题描述】:

我的应用程序将电子邮件发送到 Exchange 邮件服务器,邮件服务器配置有第三方应用程序,在该应用程序中它将电子邮件路由到代理,代理回复该电子邮件。应用程序从用于发送电子邮件的邮箱中读取代理回复。

邮件发送代码如下;

Message mimeMessage = new MimeMessage(session);
                 mimeMessage.setFrom(new InternetAddress(from));
                 mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                 mimeMessage.setSubject(subject);
                 mimeMessage.setContent(emailText,"text/plain");

                 mimeMessage.setReplyTo(replyToAddress);

                 Transport.send(mimeMessage); 

这非常有效。当收到代理回复时,应用程序将其读取为;

 if (message.isMimeType("multipart/MIXED")) 
                logger.info("Email MIME Type is: multipart/MIXED");
                MimeMultipart multipart =(MimeMultipart)message.getContent();

                logger.info("Content type = "+multipart.getContentType());
                int count = multipart.getCount();

内容类型为“多部分/混合”,但计数为 0 表示此电子邮件中没有部分。

我需要设置系统属性,

System.setProperty("mail.mime.multipart.allowempty", "true"); 

如果没有设置,multipart.getCount() 会抛出“missingBoundryException”。

为什么会这样? 可以看到agent的回复不是空的。 邮件发送的内容类型为 text/plain,为什么回复类型是 multipart/mixed?

这是由于第三方应用程序的电子邮件格式无效,解决方法是什么?

以下是代理回复的快照。

以下是原始 MIME 内容,

Received: from sociaminer.host (192.168.1.29) by thirdpartHost
 (192.168.1.53) with Microsoft SMTP Server (TLS) id 14.1.218.12; Thu, 19 Jan
 2017 17:06:26 +0500
To: hafiz <hafiz@bla.bla>
Message-ID: <hassan.MESSAGEID@bla.bla>
In-Reply-To: <CF72F94@bla.bla>
References: <CF72F945A@bla.bla>
Subject: Re: 1122+50
Content-Type: multipart/mixed;
    boundary="----=_Part_127_14151461.1484827604583"
From: <reply@bla.bla>
Return-Path: reply@bla.bla
Date: Thu, 19 Jan 2017 17:06:26 +0500
X-MS-Exchange-Organization-AuthSource: bla.bla
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 06
X-Originating-IP: [SocialMinerIP]
MIME-Version: 1.0

------=_Part_127_14151461.1484827604583
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">Reply to 50<br>

<blockquote><hr>
<b>From:</b> hafiz &lt;hafiz@bla.bla&gt;<br><b>Sent:</b> Thursday, January 19, 2017 5:05 PM<br><b>To:</b> testing2 &lt;testing2@bla.bla&gt;<br><b>Subject:</b> 1122&#43;50<br>

<html dir="ltr">
<head>

<style type="text/css" id="owaParaStyle"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">Testing 50</div>
</body>
</html>
</blockquote>
------=_Part_127_14151461.1484827604583--

JavaMail 调试输出如下所示,

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: disable AUTH=PLAIN
DEBUG IMAP: enable STARTTLS
DEBUG IMAP: trying to connect to host "Echange IP", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: protocolConnect login, host=192.168.1.53, user=hafiz@bla.bla, password=<non-null>
A1 STARTTLS
A1 OK Begin TLS negotiation now.
A2 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A2 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTHENTICATE NTLM command trace suppressed
DEBUG NTLM: type 1 message: 4E 54 4C 4D 53 53 50 00 01 00 00 00 03 A2 00 00 00 00 00 00 23 00 00 00 03 00 03 00 20 00 00 00 31 39 32 
DEBUG NTLM: type 3 message: 4E 54 4C 4D 53 53 50 00 03 00 00 00 18 00 18 00 68 00 00 00 18 00 18 00 80 00 00 00 00 00 00 00 40 00 00 00 22 00 22 00 40 00 00 00 06 00 06 00 62 00 00 00 00 00 00 00 98 00 00 00 01 82 00 00 68 00 61 00 66 00 69 00 7A 00 40 00 65 00 66 00 6C 00 61 00 62 00 2E 00 6C 00 6F 00 63 00 61 00 6C 00 31 00 39 00 32 00 3B 5E 2B 86 67 49 E3 01 C9 9E F2 CA ED 54 21 11 81 89 94 C6 EC E0 26 E3 BA DB E7 5A F4 CA 28 17 7C 0E 8A 08 18 B5 5A 4E 72 4F C5 7F 52 64 FA 76 
DEBUG IMAP: AUTHENTICATE NTLM command result: A3 OK AUTHENTICATE completed.
A4 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A4 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: connection available -- size: 1
A5 SELECT INBOX
* 40 EXISTS
* 0 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 39] Is the first unseen message
* OK [UIDVALIDITY 436] UIDVALIDITY value
* OK [UIDNEXT 46] The next unique identifier value
A5 OK [READ-WRITE] SELECT completed.
A6 SEARCH UNSEEN ALL
* SEARCH 39
A6 OK SEARCH completed.
A7 SEARCH UNSEEN ALL
* SEARCH 39
A7 OK SEARCH completed.
main INFO  emailToSms.EmailReader - 1 unread emails read from inbox.
A8 STORE 39 +FLAGS (\Seen)
* 39 FETCH (FLAGS (\Seen))
A8 OK STORE completed.
A9 FETCH 39 (BODY.PEEK[HEADER])
* 39 FETCH (BODY[HEADER] 851
MIME-Version: 1.0
Received: from HOST (IP) by HOST
 (192.168.1.53) with Microsoft SMTP Server (TLS) id 14.1.218.12; Thu, 19 Jan
 2017 17:06:26 +0500
To: hafiz <hafiz@bla.bla>
Message-ID: <hassan.B69E3DD110000159000004A73F57FEE3.1484827604448.cisco-ccp@bla.bla>
In-Reply-To: <CF72F945A1ED2E438A53A11DA9415F65A0E981@Expert.bla.bla>
References: <CF72F945A1ED2E438A53A11DA9415F65A0E981@Expert.bla.bla>
Subject: Re: 1122+50
Content-Type: multipart/mixed;
    boundary="----=_Part_127_14151461.1484827604583"
From: <testing2@bla.bla>
Return-Path: testing2@bla.bla
Date: Thu, 19 Jan 2017 17:06:26 +0500
X-MS-Exchange-Organization-AuthSource: Expert.bla.bla
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 06
X-Originating-IP: [IP]

)
A9 OK FETCH completed.
A10 FETCH 39 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 39 FETCH (ENVELOPE ("Thu, 19 Jan 2017 17:06:26 +0500" "Re: 1122+50" ((NIL NIL "testing2" "bla.bla")) NIL NIL (("hafiz" NIL "hafiz" "bla.bla")) NIL NIL "<CF72F945A1ED2E438A53A11DA9415F65A0E981@Expert.bla.bla>" "<hassan.B69E3DD110000159000004A73F57FEE3.1484827604448.cisco-ccp@bla.bla>") INTERNALDATE "19-Jan-2017 17:06:26 +0500" RFC822.SIZE 1250)
A10 OK FETCH completed.
A11 FETCH 39 (BODYSTRUCTURE)
* 39 FETCH (BODYSTRUCTURE ("multipart" "mixed" ("boundary" "----=_Part_127_14151461.1484827604583") NIL NIL 7BIT 0 NIL NIL NIL NIL))
A11 OK FETCH completed.

DEBUG IMAP: IMAPProtocol noop
A12 NOOP
A12 OK NOOP completed.

【问题讨论】:

发布回复的原始 MIME 内容,以便我们了解它有什么问题。您可以使用 message.writeTo(new FileOutputStream("msg.txt"));然后查看msg.txt.file。 @BillShannon 添加了原始 MIME 内容。 我没有发现任何明显的错误。你在使用 IMAP 吗? JavaMail debug output 显示什么? 是的,我正在使用 IMAP。添加了调试输出。 DEBUG IMAP:尝试连接到主机“ExchangeIP”,端口 143,isSSL false。 SSL 是假的,这样可以吗? 【参考方案1】:

这是 Microsoft Exchange 中的一个错误。向 Microsoft 报告此错误并尽可能升级到更新版本或更新的服务包,以防他们已经修复它。

Exchange 正在返回消息的 BODYSTRUCTURE 信息,就像它是单部分消息一样,而实际上它是多部分消息。这违反了 IMAP 协议规范。

您可以使用workaround in the JavaMail FAQ。

另外,你可能想upgrade to a newer version of JavaMail - 1.4.7 已经很老了,当前版本是 1.5.6。

【讨论】:

JavaMail 1.5.6 也有同样的问题。升级 Exchange 工作。谢谢。

以上是关于使用 JavaMail 读取电子邮件时,MimeMultipart 计数为零的主要内容,如果未能解决你的问题,请参考以下文章

尝试在 Java 中使用 Gmail API 发送电子邮件时出错

无法使用 JavaMail 读取 Outlook 邮件,而 Gmail 可以工作

在使用 JavaMail API 时,如何确保多服务器环境中的一台服务器仅读取一次电子邮件

JavaMail 读取消息

JavaMail读取邮件,如何过滤需要的邮件

JavaMail 使用 IMAP 读取最近的未读邮件