gpt4 book ai didi

java - 如何从电子邮件中获取内联图像?

转载 作者:行者123 更新时间:2023-11-30 08:51:02 31 4
gpt4 key购买 nike

我正在使用下一个代码使用 javamail API 从我的邮箱帐户下载附件和正文,它工作得很好。但是,当电子邮件中包含内联或嵌入图像时,图像不会以文本或附件文件的形式下载。我是 Java 的新手,一直在网上阅读,但没有找到易于理解的实现解决方案。有任何解决方法或代码来完成它吗?

这是我正在使用的代码:

public void processMessageBody(Message message) {
try {
Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch;
while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}

}

} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}

public void procesMultiPart(Multipart content) {

try {

for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;

o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("fileName = " + fileName);
InputStream inStream = bodyPart.getInputStream();
FileOutputStream outStream = new FileOutputStream(new File(
downloadDirectory + fileName));
byte[] tempBuffer = new byte[4096];// 4 KB
int numRead;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
inStream.close();
outStream.close();
}
// else?

}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}

}

我尝试添加下一个 if 语句以在它是内联图像时显示消息,但运气不佳:

 public void procesMultiPart(Multipart content) {

try {

for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;

o = bodyPart.getContent();
// NOT WORKING
if (o instanceof Image) {
System.out.println("procesMultiPart has Inline Images");
}
//
else if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("fileName = " + fileName);
InputStream inStream = bodyPart.getInputStream();
FileOutputStream outStream = new FileOutputStream(new File(
downloadDirectory + fileName));
byte[] tempBuffer = new byte[4096];// 4 KB
int numRead;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
inStream.close();
outStream.close();
}
// else?

}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}

}

最佳答案

下面的代码应该适合你....

private String getAttachments(Message message, HttpServletRequest request) throws MessagingException, IOException {
String contentType = message.getContentType();
String attachFiles="";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
String disposition =part.getDisposition();
String file=part.getFileName();
//External attachments
if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
// this part is attachment
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_"); //To make attachment name uniq we are adding current datatime before name.
attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName))); //To save the attachment file at specific location.
// LOG.info("\n\t Path :- " +request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/" + fileName));
}
//Inline Attachments
else if (disposition != null && Part.INLINE.equalsIgnoreCase(disposition)) {
// this part is attachment
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_"); //To make attachment name uniq we are adding current datatime before name.
// attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName))); //To save the attachment file at specific location.
// LOG.info("\n\t Path :- " +request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/" + fileName));
}
//Inline icons and smileys
else if(file != null && disposition==null)
{
String fileName = new Date().getTime()+ "_"+ part.getFileName().replaceAll("[^a-zA-Z0-9\\._]+", "_");
// attachFiles += fileName + ","; //concrete all attachment's name with comma separated.
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
"/WEB-INF/attechments/"
+ fileName)));

}
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 1);
}
return attachFiles;
}

关于java - 如何从电子邮件中获取内联图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722543/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com