1

我有一个客户端程序(使用 javax API),它将从电子邮件中下载附件。不幸的是,无法从特定的发件人处下载相同的内容。

这是将附件保存到本地磁盘的客户端代码:

String disp = p.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { 
 if ((contentType.length() >= 10) && (contentType.toLowerCase().substring(0, 10).equals("text/plain"))) {
  //handle plain text
 } else { 
  part.writeTo(System.out);
 }
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
 saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
 saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
 // handle else block
}

请给我一个下载附件的方法..

以下是我对无法下载附件的电子邮件的调查结果:

  • 'disp' 为null
  • contentType 是message/rfc822

下面是part.writeTo(System.out)的输出:

MIME-Version: 1.0
Content-Type: message/rfc822

Received: from  XXXXXX with ESMTP id XXXXXXX ;          Tue, 29 Jan 2013 13:53:39 -0500
From: XXXXXXX@XXXXX.com
To: XXXXXXX@XXXXX.com
Subject: XXXXXXX
Date: Tue, 29 Jan 2013 13:53:40 -0500
Message-ID: <XXXXX.XXXXXX-XXXXXXX.XXXXXXXX@LocalDomain>
X-Priority: 1 (High)
Return-Receipt-To: <XXXXX@XXXXXX.com>
Disposition-Notification-To: <XXXXXX@XXXXXXX.com>
MIME-Version: 1.0
X-MIMETrack: Itemize by SMTP Server on XXXXXXX/XXXXXX(Release 8.5.1FP1|January 05, 2010) at 01/29/2013 01:53:39 PM
X-Disclaimed: 56875
Content-Type: multipart/mixed; boundary="----=_NextPart_145_1257127098"


------=_NextPart_145_1257127098
Content-Type: application/pdf; name="attach1.pdf"; charset="iso-8859-1"
Content-Disposition: attachment; filename="attach1.pdf"
Content-Transfer-Encoding: base64

D1//AAVP/wCR8194f27kn/Q4yv8A8OGE/wDl3mvvIiCv3Qeevf6etAGcEgggjJPfFS7H/ut/3yf8
KNj/AN1v++T/AIUfVsT/ANA9f/wVP/5HzX3h/buSf9DjK/8Aw4YT/wCXea+8bSE4/P8AKn7H/ut/
3yf8KNjf3G/75P8AhR9WxP8A0D1//BU//kfNfeH9u5J/0OMr/wDDhhP/AJd5r7xmen480uPb/P8A
kU7Y39xv++T/AIUbH/ut/wB8n/Cj6tif+gev/wCCp/8AyPmvvD+3ck/6HGV/+HDCf/LvNfeNpMjp
kZp+x/7rf98n/Ck2N12HP+6f8KPq2J/6B6//AIKn/wDI+a+8P7dyT/ocZX/4cMJ/8u8194maQkgc
/wCP0f8ADtL9m/8A6F+D/wAAY/8A4/RRQFl2/rT/ACQf8O0v2b/+hfg/8AY//j9H/DtL9m//AKF+
D/wBj/8Aj9FFAWXb+tP8kH/DtL9m/wD6F+D/AMAY/wD4/R/w7S/Zv/6F+D/wBj/+P0UUBZdv60/y
Pg0Kc3RhcnR4cmVmDQoyNjY5Mg0KJSVFT0Y=


------=_NextPart_145_1257127098
Content-Type: application/pdf; name="attach-2.pdf"; charset="iso-8859-1"
Content-Disposition: attachment; filename="attach-2.pdf"
Content-Transfer-Encoding: base64

NBclCFEsQwtbRDmKbBROiSFXwAJrKJhPUAtYIG1qQGYhi2QojkKW7DAxMjXAkRaNTU1wpEUUGZQk
h2IabhnM1Ag00YieOXCQeNsQmNSx6DO21IOI4sqFyPowXA8PSAzXIwcxUn5DcwUOmaGXFY2BCkEc
jLyoAuKSmxeRTMWRGYGuQHM4MSKDJlUamVtiT5VmcOYwzY0mKFXVCCmEiIhurIUQij5U1yMHJKrr
0YIYUdSguwKHzNArhAyAZbk5tkKI7AIIycRhWgDh0GdkoQdRN1xzIg59w74AshiR5S7hVI693DU1
1DPGUrrmogYkin3oQYxUuiKbhltmyJW7RqZGcL+glbsGBqZGZJa9yKbiKHvJDhVDEyMcqdLIxAxH
qkSRQUl7KKbhlsFMlUATjeiZGQeJtw3M9Yyx6DO21IOI4sqMyPowXA8PSAzXIwcxUpZDcwUOmaGX
GY0s9QxAHIzMqGKuZ25CbmZEMhVHZgS6As3hxIgMmlRpaA4sgrGlSjM4c5jmRjOU6mqEFEJERDfW
QghFH6rrkQMS1fVoQYwoatBdgUNm6BVCBoZ6ZubYCiEzcgsgJBOHaQGEQ5+RhR5E3XDNiTj0DfsC
NQ0KL0luZm8gMSAwIFINCi9Sb290IDIgMCBSDQo+Pg0Kc3RhcnR4cmVmDQo0ODYzMA0KJSVFT0Y=


------=_NextPart_145_1257127098
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="iso-8859-1"

Dear XXXXXX,

Please see attached for your most recent attachments.  Please direct correspond=
ence and inquiries to accounts=XXXXX@XXXX.com.
=20
Thank you for your continued support, we appreciate it very much.
=20
=20
Sincerely,
=20
XXXXX, XXX.

------=_NextPart_145_1257127098--
4

1 回答 1

1

我有一个解决我的问题的方法。

当 disposition 为 null 且 Content-type 为 'MESSAGE/XXXX' 时,将其视为新消息并迭代其内容。

else  if(disp == null && StringUtils.isNotBlank(contentType) && contentType.toLowerCase().substring(0, 7).equals("message")) {
    IMAPNestedMessage msg = (IMAPNestedMessage) p.getContent();
    Part p2 = (Part) msg;
    Multipart mp = (Multipart) p2.getContent();
    int count = mp.getCount();
    for (int i = 0; i < count; i++){
        addAttachment(mp.getBodyPart(i), fileNames, fileName);
    }
}
于 2013-02-05T14:17:40.113 回答