如何使用 JavaMail API 读取电子邮件附件(当 contentType 为 text/plain 时)

How to read email attachment (when contentType is text/plain ) using JavaMail API

我们有一个外部应用程序,它向我们发送了一封带有附件的电子邮件。 在 contentType Multipart 的情况下:我们能够解析和处理附件。

但有时他们会发送内容类型为 text/plain 的邮件(message.getContent() 为空),我们无法从邮件对象中获取附件和电子邮件正文。

发件人无法将内容类型固定为多部分,我们必须在接收端容纳它。

我们正在使用 JavaMail API 1.5,尝试过 apache commons mail util,但只有当你在 message.getContent()

中有对象时它才有效

文件夹 emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY);

消息[]消息=emailFolder.getMessages(); System.out.println("Total Message" + messages.length);

for (int i = 0; i < messages.length; i++) { 消息消息=消息[i];

Multipart multipart = (Multipart) message.getContent();
 for(int k = 0; k < multipart.getCount(); k++){
   BodyPart bodyPart = multipart.getBodyPart(k);  
   InputStream stream = 
                     (InputStream) bodyPart.getInputStream();  
 }

}

**RAW MIME message content :**
  From: SENDER <SENDER@MyOrgLtd.com>
To: "'support@XYZ.com'" <'support@XYZ.com'>
Subject: Change Request #CHG85 02 ATTACHMENT
Thread-Topic: Change Request #CHG85 02 ATTACHMENT
Thread-Index: AdVQ+bdv3Fd+yaP6Qr2RCdQvPsvI9Q==
Date: Mon, 12 Aug 2019 10:37:23 +0000
Message-ID: <1dcdc97a916b4f929414d0d4b6703397@DELHIXCHMBX003.MyOrgLtd.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: yes
X-MS-TNEF-Correlator: 
dlp-product: dlpe-windows
dlp-version: 11.1.0.61
dlp-reaction: no-action
x-mcafeedlp-tagged: True
x-ms-exchange-transport-fromentityheader: Hosted
x-originating-ip: [IP REMOVED]
x-tm-onpremattruleprocessed: TRUE
Content-Type: multipart/mixed;
    boundary="_002_1dcdc97a916b4f929414d0d4b6703397DELHIXCHMBX003MyOrgLtd_"
MIME-Version: 1.0
X-EOPAttributedMessage: 0
X-MS-PublicTrafficType: Email

X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam-Message-Info:  fL+9KaLob6WaIw3QlMjrWkMfgsC6D53Vr10xvjo/DWwlRA3ZuQ0emJtxG2R1r3GXNuLYK7l6vjv/buJaaPhR7VW5qdysbinenPJjyOIwCcTuBCAm1nAtlEWJqRzIJT0n7oxDQvh7pH+mIm7yK0BwYX8nJyfg2CSot7is9h/Xbk/uwYow4RW9IuSq5ioMCPSt+zRzdfbJ76DIPvne4FYRy+D8Xbe4RBMcf6u7wvtQW3n86JtRUVz1EjDGmIA6ZfyHXtYf3Q09VKfyMg6wa7KOWJaiU+6HJCY4Jevdxgy75xvR+56PCQ1dV0QgeMsEqVuKrM0YndKVKrF3u08rO7PQoTpv37z4xqRyTNRpTwLXeuQWi6tBFRk3HjrbBKBaRpme1On2cYRFBCiXMLMxBfpNIaR4lolZ0MPL/h3UANKY7r4=
X-OriginatorOrg: MyOrgLtd.com
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2019 10:37:25.6885
 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: 8137d2d0-3ec5-4a61-f117-08d71f111117
X-MS-Exchange-CrossTenant-Id: edf442f5-b994-4c86-a131-b42b03a16c95
X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=edf442f5-4c86-a131;Ip=[IP RMOVED];Helo=[MyEmail.MyOrgLtd.com]
X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem
X-MS-Exchange-Transport-CrossTenantHeadersStamped: BMXPR01MB3830
X-CLX-Shades: MLX
X-CLX-Response: 1TFkXExoRCkx6Fx4SEQpZRBdlfmFZE0V5WUNGUBEKWFgXaWZ9aWdnaUtyflI RCnhOF20SRWsbeENtRk5hEQp5TBdrf0BAcxJBGGIcaBEKeUMXaVNsSG1kWF5yZhoRCkNIFwcYGR oRCkNZFxsfGBEKWU0XZ2ZyEQpZSRcacRoQGncGHRpxHhAadwYYGgYaEQpZXhdobnkRCklGF0teX
 nVCRVleT04RCkNOF3l1T25TfGVCe1xEfF1NWlBbbx9abRtsc0BbY1lGXlATEQpYXBcfBBoEGxIb BxNMGhwTThIfBRsaBBsaGgQeEgQcEBseGh8aEQpeWRd/Ql9zbBEKTVwXGx0eEQpMWhdoaU1raxE KTEYXb2tra2traxEKQk8XaUVwGwFzWHhPG0gRCkNaFx4aBBsaHQQbGRIEGxkbEQpCXhcbEQpEXh
 cYEQpCXBcaEQpCRRdsSWtYGWwcf2RTSxEKQk4XbRJFaxt4Q21GTmERCkJMF2lmfWlnZ2lLcn5SE QpCbBdhbXkfU2Z4b25DSBEKQkAXZmlOYEBgbVNLW3IRCkJYF3pAbVgfbnhkHUUfEQpNXhcbEQpa WBcYEQpwaBdiXEcaUG98Q15ERxAZGhEKcGgXYWFAa0FNbENaQBgQHBoRCnBoF2UFeGZtGXppaB5
 pEB0aEQpwaBdjeEllXmZ8fUkSBRAdGhEKcGgXY0VPSWJSfWBdUEAQGRoRCnBrF21pQXNffU8BBQ FuEBkaEQpwSxdhUBIbAU9saERdBRAZGhEKcGsXY2dsAWlvYXN5YnkQGRoRCnBsF2drQ2lPeV9DR xJBEBkaEQpwQxdsSx1AEl9bTQVrRBAcHREKbX4XGxEKWE0XSxEg
X-Proofpoint-SPF-Result: pass
X-Proofpoint-SPF-Record: v=spf1 mx ip4:IP RMOVED ip4:IP RMOVED
 ip4:IP RMOVED ip4:IP RMOVED ip4:IP RMOVED
 ip4:IP RMOVED include:spf2.MyOrgLtd.com
 include:spf.protection.outlook.com -all
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-08-12_04:,,
 signatures=0
X-Proofpoint-Spam-Reason: safe
--_002_1dcdc97a916b4f929414d0d4b6703397DELHIXCHMBX003MyOrgLtd_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

hi User,
    We have received the data, will process and inform you.

Thank you,
admin


--_002_1dcdc97a916b4f929414d0d4b6703397DELHIXCHMBX003MyOrgLtd_
Content-Type: application/octet-stream; name="testData.csv"
Content-Description: testData.csv
Content-Disposition: attachment; filename="testData.csv"; size=393;
    creation-date="Mon, 12 Aug 2019 10:35:38 GMT";
    modification-date="Mon, 12 Aug 2019 10:35:38 GMT"
Content-Transfer-Encoding: base64

IkNpcmN1aXQgIiwiU2V2ZXJpdHkiCiJDaXJjdWl0OSAgICAvU0EgIC8iLCJPdXRhZ2UiCiJDaXJjdWl0MTQgICAgL1NMICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDggICAgL1NCICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDUgICAgL1NCICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDYgICAgL1NUICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDcgICAgL1NCICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDIgICAgL1NHICAvIiwiT3V0YWdlIgoiQ2lyY3VpdDUxICAgIC9TQiAgLyIsIk91dGFnZSIKIkNpcmN1aXQxICAgIC9TTSAgLyIsIk91dGFnZSIKIkNpcmN1aXQ0ICAgIC9TQiAgLyIsIk91dGFnZSI=

--_002_1dcdc97a916b4f929414d0d4b6703397DELHIXCHMBX003MyOrgLtd_--

如果内容类型是text/plain,则没有附件。

如果 message.getContent() 返回 null,则可能也没有消息内容,或者消息的格式有问题。 Post 邮件的原始 MIME 内容,我可以帮助您找出邮件中的错误。