我是java邮件轮询的新手,如果用户向对方发送邮件,我会创建一种会话应用程序,然后我从他们那里读取邮件并在会话中将其作为新邮件发布。
现在的问题是,如果有笑脸,内联或嵌入图像该怎么办。例如在gmail邮件中我们也可以发送表情符号,现在如何阅读那个微笑并发布到页面上。请给我一些适当的解决方案。
答案 0 :(得分:2)
我找到了从邮件中下载内联图片+图标的解决方案。
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;
}