java邮件轮询从邮件中读取内嵌或嵌入的图像(笑脸) [英] java mail polling read inline or embedded images (smileys) from the mail

查看:170
本文介绍了java邮件轮询从邮件中读取内嵌或嵌入的图像(笑脸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java邮件轮询中是新的,我已经创建了一种类型的会话应用程序,如果用户发送邮件给对方,然后我从他们那里读取邮件并发布为对话中的新消息。



现在问题是,如果有笑脸,内嵌或嵌入图像,该怎么办?例如在Gmail邮件中我们也可以发送笑脸,现在怎么读这个微笑,并发贴到页面。请给我一些正确的解决方案。



解决方案

我已经找到解决方案从邮件中下载内联图片+图标。

  private String getAttachments(消息消息,HttpServletRequest请求)throws MessagingException,IOException {
String contentType = message.getContentType();
String attachFiles =;
if(contentType.contains(multipart)){
//内容可能包含附件
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();
//外部附件
if(disposition!= null&&&& Part.ATTACHMENT.equalsIgnoreCase(disposition)){
//这部分是附件
String fileName = new Date()。getTime()+_+ part.getFileName()。replaceAll([^ a-zA-Z0-9\\ ._] +,_); //要使附件名称为uniq,我们在名称之前添加当前的datatime。
attachFiles + = fileName +,; //具体所有附件的名称用逗号分隔。
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
/ WEB-INF / attechments /
+ fileName))); //将附件文件保存在特定位置。
// LOG.info(\\\
\t Path: - + request.getSession()。getServletContext()。getRealPath(/ WEB-INF / attechments /+ fileName));
}
//内联附件
else if(disposition!= null&&&&PartitionIn.ELINE.equalsIgnoreCase(disposition)){
//这部分是附件
String fileName = new Date()。getTime()+_+ part.getFileName()。replaceAll([^ a-zA-Z0-9\\ ._] +,_) ; //要使附件名称为uniq,我们在名称之前添加当前的datatime。
// attachFiles + = fileName +,; //具体所有附件的名称用逗号分隔。
part.saveFile(new File(request
.getSession()
.getServletContext()
.getRealPath(
/ WEB-INF / attechments /
+ fileName))); //将附件文件保存在特定位置。
// LOG.info(\\\
\t Path: - + request.getSession()。getServletContext()。getRealPath(/ WEB-INF / attechments /+ fileName));
}
//内联图标和笑脸
else if(file!= null&& disposition == null)
{
String fileName = new Date ).getTime()+_+ part.getFileName()。replaceAll([^ a-zA-Z0-9\\\ ._] +,_);
// attachFiles + = fileName +,; //具体所有附件的名称用逗号分隔。
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;
}


I am new in java mail polling and i have create one type of conversation application in this if user send mail to each other then i read that from them mail and post as new message in conversation.

now issue is that, what to do if there is smileys, inline or embedded images. for example in gmail mail we can send smileys also now how read that smile and post on to the over page. please give me some proper solution for this.

解决方案

I have found the solution to download the inline images + icons from the mail.

  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邮件轮询从邮件中读取内嵌或嵌入的图像(笑脸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆