我正在尝试从邮件消息字符串中获取附件。我假设最好的方法是获取原始字符串,将其转换为javax邮件消息,然后按照this教程。我找不到一种方法来获取原始字符串并转换为javax邮件消息。附件部分的使用时间要长得多,但为了简单起见,我将其缩短了。
示例字符串:
X-DateCreated: 7/11/2014 8:12:53 PM (UTC)
X-Received: Secure delivery via Web Service
Thread-Topic: Test attachment
Date: Fri, 11 Jul 2014 16:12:53 -0400
Message-ID: <218685@CM>
thread-index: Ac+hJI371nbUghSdS9eoy8ESMrck3w==
From: <---->
To: <---->
Subject: Test attachment
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_55BE_01CFA103.0703D820"
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.2.9200.16384
This is a multi-part message in MIME format.
------=_NextPart_000_55BE_01CFA103.0703D820
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Test attachment.
------=_NextPart_000_55BE_01CFA103.0703D820
Content-Type: application/octet-stream;
name="Copyright.docx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Copyright.docx"
UEsDBBQABgAIAAAAIQCR/hdIvQEAABcJAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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qsMw//bXxb0X7PoSJnrxW6p
TaWb9NCi+mW0heacQp4Zi4tyZZBfkGLemI3IOp5HW0K35qyS6YwdZufKYsxX87UhNQ2b0wol0BFA
1Cf7lOmRsyBrsVhpzrDFeb1aKhfcguV6VLasq4XRctGaeGZL768S7ZbdbfTTdKjXipJapqpcqbrI
0i6bdNgIYN0SPcczerjFc6Y1Ueq4oNaCUY5csHzWzYXiss8wpGxScjPlL50SMcx2m+gSWPLKakiW
1VzPKldGM6WbyRIRwFa7Pu3N7RXJekO7EJTERd1v6ly5KKbZCdtq56TFPC1M7Uw6kx74E6Ga1fDO
pKExtVQ47c/EoJsezczRMFtLLiOAJb+nKZUpOk10WWqSAexnk8l5ld4XFp1KtgpJDAqr1ITCqsDz
PaJkyYXqrGKXrAigXKiPx1V+Ol/RVSNMsLN+Q9B7eWm+MEeimRih+Zlb8Nm+OalKXbS/xHUPr6Ya
TNX25LacbGZTVlJPGlUTmwoxhZ0i7tcDql0aqomByQ51vZLRxrzFt+RZB3WcFCfnjFV+kdKzeUGV
cbJQ9fvG1MrN86TlVaZZ2RyMF926Oe1HAHG8b4qehI/71RCzsKDJEmhSbkyI+bxfQxdYZiinSFpt
gksuOUr6PIOLKN9MC6gt1rgOMaKcrK41V56DmbGUuzSW4ieJZkbCpEyCmYVJNalWZbzUJ0OzIRv5
5DiwU8o0305qHuEqs3nuGJ8jgMUmI+RDRSzDc1yKg/0DXhwUSqsK3l7KqDDrL8mU3F36nVoQ4oDX
upCY6wsWnQXGYjzqL1KGoIbDeayH4XDWTA+r/W6f4ilxUDIHWFZvjUlRbsjLFZ3MlUvFgoBqYyut
答案 0 :(得分:2)
好一段时间后,迈克回答我提出了这个解决方案。下面的代码将采用一个字符串,将其转换为javax Message对象,然后解析它以提取各种字段并显示它们。希望这也有助于其他人!
<强> CODE:强>
try {
InputStream is = new ByteArrayInputStream(receivedMessage.getBytes(StandardCharsets.UTF_8));
MimeMessage mime = new MimeMessage(null, is);
String finalMessage = "From: " + mime.getFrom()[0].toString() + "\n"
+ "To: " + mime.getRecipients(RecipientType.TO)[0] + "\n";
for (int i = 1; i < mime.getRecipients(RecipientType.TO).length;i++)
finalMessage = finalMessage + mime.getRecipients(RecipientType.TO)[i] + "\n";
if (mime.getRecipients(RecipientType.CC) != null && mime.getRecipients(RecipientType.CC).length != 0) {
finalMessage = finalMessage + "CC: " + mime.getRecipients(RecipientType.CC)[0] + "\n";
for (int i = 1; i < mime.getRecipients(RecipientType.CC).length;i++)
finalMessage = finalMessage + mime.getRecipients(RecipientType.CC)[i] + "\n";
}
if (mime.getRecipients(RecipientType.BCC)!= null && mime.getRecipients(RecipientType.BCC).length != 0) {
finalMessage = finalMessage + "BCC: " + mime.getRecipients(RecipientType.BCC)[0] + "\n";
for (int i = 1; i < mime.getRecipients(RecipientType.BCC).length;i++)
finalMessage = finalMessage + mime.getRecipients(RecipientType.BCC)[i] + "\n";
}
finalMessage = finalMessage + "Subject: " + mime.getSubject() + "\n"
+ "Sent: " + mime.getSentDate() + "\n";
if (mime.getReceivedDate() != null)
finalMessage = finalMessage + "Received: " + mime.getReceivedDate() + "\n\n";
else
finalMessage = finalMessage + "\n";
// suppose 'message' is an object of type Message
String contentType = mime.getContentType();
if (contentType.contains("multipart")) {
// this message may contain attachment
Multipart multiPart = (Multipart) mime.getContent();
String attachments = "";
String messageContent = "";
for (int i = 0; i < multiPart.getCount(); i++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
// code to save attachment...
attachments = attachments + "Attachment: "+part.getFileName()+"\n"; //Turn this into a link the user can click to download
}
else {
messageContent = messageContent + part.getContent().toString() + "\n";
}
}
finalMessage = finalMessage + messageContent + "\n" + attachments;
}
message.setText(finalMessage);
} catch (Exception e4) {
e4.printStackTrace();
}
答案 1 :(得分:1)
这里有example从文件读取消息,它可能在设置MimeMessage并从字符串而不是从该示例中的File创建InputStream时很有用。
InputStream stream = new ByteArrayInputStream(mailString.getBytes(StandardCharsets.UTF_8));
OR
您可以创建DataHandler对象。请参阅此SO thread
然后创建一个MimeMessage并使用DataHandler调用它的setContent 方法。