用Java提取内联电子邮件附件
我正在使用以下用Java提取内联电子邮件附件,java,jakarta-mail,email-attachments,apple-mail,Java,Jakarta Mail,Email Attachments,Apple Mail,我正在使用以下Java代码尝试提取电子邮件附件: private static List<File> extractAttachment(Message message) { List<File> attachments = new ArrayList<File>(); try { Multipart multipart = (Multipart) message.getContent(); for (int
Java
代码尝试提取电子邮件附件:
private static List<File> extractAttachment(Message message) {
List<File> attachments = new ArrayList<File>();
try {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
continue; // dealing with attachments only
}
InputStream is = bodyPart.getInputStream();
String filePath = "/tmp/" + bodyPart.getFileName();
System.out.println("Saving: " + filePath);
File f = new File(filePath);
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
} catch (IOException | MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return attachments;
}
私有静态列表提取附件(消息){
列表附件=新的ArrayList();
试一试{
Multipart Multipart=(Multipart)message.getContent();
对于(int i=0;i
但是,我总是得到bodyPart.getDisposition():null
。我该如何提取内联附件
谢谢
注意:我正在使用Mac电脑上的
Apple Mail
客户端发送带有附件的测试电子邮件。然而,电子邮件客户端不应该引起关注。我得到了它,事实上,我必须进行一些“递归”,检查嵌套内容并检查Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition()
)
下面是代码,认为对某人有用:
private static List<File> extractAttachment(Multipart multipart) {
List<File> attachments = new ArrayList<File>();
try {
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
extractAttachment((Multipart) bodyPart.getContent());
}
System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
if (!Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition())) {
continue; // dealing with attachments only
}
InputStream is = bodyPart.getInputStream();
String filePath = "/tmp/" + bodyPart.getFileName();
System.out.println("Saving: " + filePath);
File f = new File(filePath);
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
} catch (IOException | MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return attachments;
}
私有静态列表提取附件(多部分多部分){
列表附件=新的ArrayList();
试一试{
对于(int i=0;i
我明白了,实际上我必须执行一些“递归”,检查嵌套内容并检查Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition()
)
下面是代码,认为对某人有用:
private static List<File> extractAttachment(Multipart multipart) {
List<File> attachments = new ArrayList<File>();
try {
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
extractAttachment((Multipart) bodyPart.getContent());
}
System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
if (!Part.INLINE.equalsIgnoreCase(bodyPart.getDisposition())) {
continue; // dealing with attachments only
}
InputStream is = bodyPart.getInputStream();
String filePath = "/tmp/" + bodyPart.getFileName();
System.out.println("Saving: " + filePath);
File f = new File(filePath);
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
} catch (IOException | MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return attachments;
}
私有静态列表提取附件(多部分多部分){
列表附件=新的ArrayList();
试一试{
对于(int i=0;i
当从没有正文内容但带有附件的mac发送邮件时,我也遇到了类似的问题。当迭代初始邮件内容时,附件没有显示出来,并且必须递归查找
我把对我有用的东西贴在下面
private Map<String, String> extractAttachments(Multipart multiPart, Map<String, String> attachmentAndName) {
try {
for (int i = 0; i < multiPart.getCount(); i++) {
BodyPart part = multiPart.getBodyPart(i);
if (part.getContent() instanceof Multipart) {
attachmentAndName = extractAttachments((Multipart) part.getContent(), attachmentAndName);
} else if (part instanceof MimeBodyPart) {
MimeBodyPart mimeBodyPart = (MimeBodyPart) part;
String fileName = createUniqueFileName(mimeBodyPart.getFileName());
if (Part.ATTACHMENT.equalsIgnoreCase(mimeBodyPart.getDisposition())) {
String attachmentAsString = IOUtils.toString(mimeBodyPart.getInputStream(), StandardCharsets.UTF_8);
attachmentAndName.put(fileName, attachmentAsString);
if (SAVE_ATTACHMENTS_LOCALLY) {
saveFile(mimeBodyPart, fileName);
}
}
}
}
} catch (IOException | MessagingException e) {
logger.error("Failed to process attachment. Continuing to process other message parts in search of attachments...");
}
return attachmentAndName;
}
私有映射提取附件(多部分、多部分、映射附件和名称){
试一试{
对于(int i=0;i