JAVA解析eml格式的郵件


依賴

<!-- apache文件操作庫 -->
<dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.11.0</version>
</dependency>
<!-- 郵件解析基本庫 -->
<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.7</version>
</dependency>
<!-- apache對javax.mail庫的封裝 -->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-email</artifactId>
  <version>1.5</version>
</dependency>

代碼

public class Test {
    public static void main(String[] args) {
        String dir = "D:/test/";
        try(InputStream inputStream = new FileInputStream(dir+"test3.eml")) {
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage msg = new MimeMessage(session, inputStream);
            MimeMessageParser parser = new MimeMessageParser(msg).parse();
            //郵件唯一id
            String messageId = parser.getMimeMessage().getMessageID();
            System.out.println(messageId);
            //發件人
            String from = parser.getFrom();
            System.out.println(from);
            //收件人列表
            List<Address> toArray = parser.getTo();
            System.out.println(toArray);
            //抄送人列表
            List<Address> ccArray = parser.getCc();
            System.out.println(ccArray);
            //密送人列表
            List<Address> bccArray = parser.getBcc();
            System.out.println(bccArray);
            //郵件發送時間
            Date sendDate = parser.getMimeMessage().getSentDate();
            System.out.println(sendDate);
            //郵件主題
            String subject = parser.getSubject();
            System.out.println(subject);
            //獲取正文
            String html = parser.getHtmlContent();
            System.out.println(html);
            //所有文件,包括附件和正文中的圖片等文件
            List<DataSource> dataSources = parser.getAttachmentList();
            //獲取不到html內容時,則獲取非html文本內容
            if (html == null || html.length()==0) {
                String plain = parser.getPlainContent();
                System.out.println(plain);
            }else {
                //獲取正文中的圖片等文件
                Collection<String> contentIds = parser.getContentIds();
                for (String contentId : contentIds) {
                    DataSource contentFile = parser.findAttachmentByCid(contentId);
                    dataSources.remove(contentFile);
                    String name = contentFile.getName();
                    InputStream is = contentFile.getInputStream();
                    FileUtils.copyInputStreamToFile(is,new File(dir+name));
                    html = html.replace("cid:"+contentId,name);
                }
                FileUtils.writeStringToFile(new File(dir+"test.html"),html, Charset.defaultCharset());
            }
            for (DataSource dataSource : dataSources) {
                String name = dataSource.getName();
                System.out.println(name);
                InputStream is = dataSource.getInputStream();
                FileUtils.copyInputStreamToFile(is,new File(dir+name));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM