因此,我试图制作一个Drag.drop应用程序,该应用程序使用.eml文件并保存各种位。
附件部分没有给我任何问题,但是包含的邮件正文和图像是。
在我的代码中,我正在使用javax.mail库。
使用方法

MimeBodyPart.getDisposition()


当我打印出字符串时,它会正确地返回“附件”,但是对于内联部分,它会返回“空”并将图像和主体聚集在一起。
在.eml文件(使用Lotus Notes生成)中,您可以清楚地看到每个部分的Content-Disposition / ID / Type / Transfer-encoding...。

谁能告诉我我想念的东西吗?

这是处理此问题的循环:

for (File file: files) {
    System.out.println(file.getPath());
    Properties props = System.getProperties();
    props.put("mail.host", "smtp.dummydomain.com");
    props.put("mail.transport.protocol", "smtp");

    Session mailSession = Session.getDefaultInstance(props, null);
    try {
        InputStream source = new FileInputStream(file.getPath());
        MimeMessage message = new MimeMessage(mailSession, source);
        Multipart multiPart = (Multipart) message.getContent();
        for (int i = 0; i < multiPart.getCount(); i++) {
            MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
            System.out.println(part.getDisposition());
            if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                System.out.println("part " + i);
                part.saveFile("C:/" + part.getFileName());
            }

        }

    } catch (MessagingException | IOException e) {

        e.printStackTrace();
    }
}


由于我得到了路径名并保存了附件,因此文件对象已正确初始化。

非常感激任何的帮助。

以下是对.eml文件的稍作修改的示例。内联图像和附件缩短到仅几行,以便我可以在此处发布:

From: [email protected]
X-Notes-Item: Mon, 8 May 2017 10:59:55 +0200;
 type=400; name=OriginalModTime
X-Notes-Item: Memo;
 name=Form
X-Notes-Item: stdNotesLtr25;
 name=Logo
X-Notes-Item: ;
 name=dLogo
X-Notes-Item: True;
 name=useApplet
X-Notes-Item: 1;
 name=DefaultMailSaveOptions
X-Notes-Item: 1;
 name=ExpandPersonalGroups
X-Notes-Item: ;
 name=tmpImp
X-Notes-Item: ;
 name=Sign
X-Notes-Item: Stack Overflow saved my life.;
 flags=6; name=$Abstract
X-Notes-Item: Vertraulich;
 name=ConfidentialString
X-Notes-Item: ;
 name=Classification
Subject: Example email
X-KeepSent: 2C23BC4F:883C3D54-C125811A:0030D552;
 type=4; name=$KeepSent
To: [email protected]
X-Notes-Item: ;
 flags=45; name=InetCopyTo
X-Notes-Item: 1;
 name=$StorageTo
X-Mailer: IBM Notes Release 9.0.1FP7 August 18, 2016
Message-ID: <OF2C23BC4F.883C3D54-ONC125811A.0030D552-C125811A.00316E89@LocalDomain>
Sender: [email protected]
Date: Mon, 8 May 2017 10:59:55 +0200
X-Notes-Item: 0;
 name=Encrypt
X-Notes-Item: CN=N Jun/OU=GU/OU=AT/O=domain;
 type=501; flags=44; name=$UpdatedBy
X-Notes-Item: 2C23BC4F:883C3D54-C125811A:0030D552;
 type=4; name=$Orig
X-Notes-Item: ;
 type=501; name=Categories
X-Notes-Item: ;
 type=401; name=$Revisions
X-Notes-Item: CN=S-NOTESGU001/OU=AT/O=domain;
 type=501; flags=0; name=RouteServers
X-Notes-Item: 08-May-2017 10:59:55 CEDT/08-May-2017 10:59:55 CEDT;
 type=401; flags=0; name=RouteTimes
X-Notes-Item: Mon, 8 May 2017 10:59:55 +0200;
 type=400; name=DeliveredDate
X-Notes-Item: 2C23BC4F:883C3D54-C125811A:0030D552;
 type=4; name=$TUA
X-Notes-Item: 1;
 name=$NoteHasNativeMIME
X-Notes-Item: CN=N Jun/OU=GU/OU=AT/O=domain@domain;
 name=OriginalTo
X-Notes-Item: CN=N Jun/OU=GU/OU=AT/O=domain;
 name=OriginalFrom
X-Notes-Item: CN=someone/O=domain;
 name=OriginalPrincipal
X-MIMETrack: Serialize by notes2.exe on N Jun/GU/AT/domain(Release 9.0.1FP7|August
 18, 2016) at 08.05.2017 11:40:46
MIME-Version: 1.0
Content-type: multipart/mixed;
    Boundary="0__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2"

--0__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2
Content-type: multipart/related;
    Boundary="1__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2"

--1__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2
Content-type: text/html; charset=US-ASCII
Content-Disposition: inline

<html><body>
<table id="memoReadTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr valign="top"><td width="0%"><img width="1" height="1" src="/icons/ecblank.gif" border="0" alt=""></td><td width="100%"><img width="1" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td><td width="1%"><img width="53" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td></tr>

<tr valign="top"><td width="1%"><img width="90" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td><td width="100%"><img width="1" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td><td width="1%"><img width="53" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td></tr>

<tr valign="top"><td width="1%"><img width="102" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>

<ul style="padding-left: 4pt"><font size="1" color="#929292" face="sans-serif">An:</font></ul>
</td><td width="100%" valign="middle"><img width="1" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
<font size="1" face="sans-serif">[email protected]</font></td><td width="1%"><img width="53" height="1" src="/icons/ecblank.gif" border="0" alt=""><br>
</td></tr>
</table>
<font size="2" face="sans-serif">Stack Overflow saved my life.</font><br>
<br>
<img src="cid:0__=4EBB0B89DFA353C28f9e8a93df93869091@local" width="1030" height="282"><br>
<i>(Siehe angeh&#228;ngte Datei: DT between trees.jpg)</i></body></html>
--1__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2
Content-type: image/gif;
    name="02629422.gif"
Content-Disposition: inline; filename="02629422.gif"
Content-ID: <0__=4EBB0B89DFA353C28f9e8a93df93869091@local>
Content-transfer-encoding: base64

R0lGODlhBgQaAecAAP///xgQGCAYIPDw8Ojo6CggKDAoMPj48JCQkEA4QGBYYODg4NjY2PD4+Gho
aNDQ0CAQGKCgoBgQIMjIyDgwOMDAwKioqLi4uBgQKCAYKLCwsOjw+Pj46FBIUOj4+BgQMCgQGCgY

...........Shortened............

QAgMLxngkLgwYcIFO1OWQHbZObbs2bQpHligJsKPEhEuMJhaO7jw4cSLGz8+2EkYLTIC9QACaUoW
FRACGDFAgQ9S5Ny7e/8OPrz48eTLkz8wYADwh+nTrzcPP778+fTr27+PP7/+/fz7+/8PYIACDkhg
gQYeiGCCCi4SyGCDDj4IYYQSTkhhhRZeOFJAADs=


--1__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2--


--0__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2
Content-type: image/jpeg;
    name="DT between trees.jpg"
Content-Disposition: attachment; filename="DT between trees.jpg"
Content-ID: <C3__=4EBB0B89DFA353C28f9e8a93df93869091@local>
Content-transfer-encoding: base64

/9j/4V/+RXhpZgAASUkqAAgAAAAMAA4BAgAgAAAA8BUAAA8BAgAYAAAAEBYAABABAgARAAAAKBYA
ABIBAwABAAAAAQAAABoBBQABAAAApBAAABsBBQABAAAArBAAACgBAwABAAAAAgAAADEBAgAgAAAA

.................Shortened...............................................

M0+RCg4q1qzknLUqyTbFOT0rK87zXOPWk11Mmf/V6rB29earngtnmvwGL1P02EtTOuJGY4FRAFQM
810dDrU1YXzATjNaFqFHJqXsS5XNaFl2jnipJmDIdtYqN2YSZ//W3pgd1Y13aPK5Jr8Op6I/SpS6
C29kUIxWtaQBW9TUVJ3MXM3rYKAPpUz4IxXI1qZt3ZCVXn1qtMUweBWdtTRH/9k=

--0__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2--

最佳答案

您的示例.eml文件显示了嵌套在外部multipart / mixed内部的multipart /相关文件。 msgshow.java sample program显示消息具有以下结构:

This is the message envelope
---------------------------
FROM: [email protected]
REPLY TO: [email protected]
TO: [email protected]
SUBJECT: Example email
SendDate: Mon May 08 01:59:55 PDT 2017
FLAGS:
X-Mailer: IBM Notes Release 9.0.1FP7 August 18, 2016
CONTENT-TYPE: multipart/mixed;
        boundary="0__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2"
This is a Multipart
---------------------------
  CONTENT-TYPE: multipart/related;
        boundary="1__=4EBB0B89DFA353C28f9e8a93df938690918c4EBB0B89DFA353C2"
  This is a Multipart
  ---------------------------
    CONTENT-TYPE: text/html; charset=US-ASCII
    ---------------------------
    CONTENT-TYPE: image/gif; name=02629422.gif
    FILENAME: 02629422.gif
    ---------------------------
  CONTENT-TYPE: image/jpeg; name="DT between trees.jpg"
  FILENAME: DT between trees.jpg
  ---------------------------


您的示例代码仅处理外部multipart / mixed。外部multipart / mixed还包括一个image / jpeg附件,您的代码应在其中找到。内部multipart / related包含一个image / gif内联组件,您的代码将永远看不到它。

我看不到任何原因使您的代码在处理image / jpeg部分时应为null。您是否正在使用IMAP访问邮件? JavaMail debug output显示什么?

关于java - MimePartBody.getDisposition()为嵌入式零件返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43834042/

10-13 01:11