使用java mail api提取内联附件?

时间:2017-08-10 03:10:01

标签: java email javamail imap

我可以使用以下代码检索常规附件。 但不是内联附件,如粘贴在电子邮件正文中的图像。 我的目标是在本地文件夹中下载常规附件和内联附件(粘贴在正文中)。

请帮忙。

提前致谢。!!

public class GetEmails3 {
     public GetEmails3() {}
     private static void deleteEmails(String protocol, String host, final String userName, final String password, String provider)
     throws Exception {
         //Start
         Properties properties;
         Session session;
         properties = new Properties();
         properties.setProperty("mail.host", "192.168.2.40");
         //properties.setProperty("mail.port", "995");
         properties.setProperty("mail.transport.protocol", "imap");
         session = Session.getInstance(properties, new Authenticator() {
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(userName, password);
             }
         });
         try {
             Store store = session.getStore("imap");
             store.connect();
             Folder inboxFolder = store.getFolder("Inbox");
             inboxFolder.open(Folder.READ_ONLY);
             System.out.println("Connected successfully ");
             Message[] messages = inboxFolder.getMessages();
             int messageCount = messages.length;
             System.out.println("Total Messages:- " + messageCount);
             for (int i = 0; i < messages.length; i++) {
                 Message message = messages[i];
                 String messageContent = "";
                 String attachFiles = "";
                 String contentType = message.getContentType();
                 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();
                         //External attachments
                         if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                             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("TestAttachments/" + 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 nam                
                             part.saveFile("TestAttachments/" + fileName); //To save the attachment file at specific location.
                         } //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("TestAttachments/" + fileName);
                         }
                     }
                 }
             } //message for end
         } catch (MessagingException e) {
             System.out.println("Error when creating the email store ");
             throw new Exception("Error when creating the email store ", e);
         }
         //End
     }
 }

0 个答案:

没有答案