public static void main(String args[]) { Properties props = new Properties(); // 參數配置 props.setProperty("mail.transport.protocol", "smtp"); // 使用的協議(JavaMail規範要求) props.setProperty("mail.smtp.host", "smtp.exmail.qq.com"); // 發件人的郵箱的SMTP服務器地址 props.setProperty("mail.smtp.auth", "true"); // 須要請求認證 // PS:某些郵箱服務器要求SMTP鏈接須要使用SSL安全認證(爲了提升安全性,郵箱支持SSL鏈接,也能夠本身開啓), // 若是沒法鏈接郵件服務器,仔細查看控制檯打印的 log,若是有有相似"鏈接失敗,要求 SSL安全鏈接"等錯誤, // 開啓 SSL安全鏈接 // SMTP服務器的端口(非 SSL鏈接的端口通常默認爲 25,能夠不添加,若是開啓了SSL鏈接, // 須要改成對應郵箱的SMTP服務器的端口,具體可查看對應郵箱服務的幫助, // QQ郵箱的SMTP(SLL)端口爲465或587 final String smtpPort = "465"; props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", smtpPort); Session session = Session.getDefaultInstance(props); session.setDebug(false); try { Store store = session.getStore("imap"); store.connect("imap.exmail.qq.com", "it01@tofba.com", "*****");// change the user and password accordingly Folder folder = store.getFolder("inbox"); if (!folder.exists()) { System.out.println("inbox not found"); System.exit(0); } folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); if (messages == null || messages.length <= 0) { System.out.println("this inbox no messages"); System.exit(0); } for (Message message : messages) { String subject = message.getSubject(); if (StringUtils.isNotBlank(subject)) { System.out.println("subject:" + subject); } /* * 解析郵件內容 */ Object content = message.getContent(); if (null != content) { if (content instanceof MimeMultipart) { MimeMultipart multipart = (MimeMultipart)content; parseMultipart(multipart); } } } } catch (Exception e) { e.printStackTrace(); } } /** * 對複雜郵件的解析 * * @param multipart * @throws MessagingException * @throws IOException */ public static void parseMultipart(Multipart multipart) throws MessagingException, IOException { int count = multipart.getCount(); for (int idx = 0; idx < count; idx++) { BodyPart bodyPart = multipart.getBodyPart(idx); System.out.println(bodyPart.getContentType()); if (bodyPart.isMimeType("text/plain")) { System.out.println(bodyPart.getContent()); } else if (bodyPart.isMimeType("text/html")) { System.out.println(bodyPart.getContent()); } else if (bodyPart.isMimeType("multipart/*")) { Multipart mpart = (Multipart)bodyPart.getContent(); parseMultipart(mpart); } else if (bodyPart.isMimeType("application/octet-stream")) { String disposition = bodyPart.getDisposition(); System.out.println(disposition); if (StringUtils.isNotBlank(disposition) && disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) { String fileName = bodyPart.getFileName(); InputStream is = bodyPart.getInputStream(); copy(is, new FileOutputStream("D:\\tofba\\email\\" + fileName)); } } } } /** * 文件拷貝,在用戶進行附件下載的時候,能夠把附件的InputStream傳給用戶進行下載 * * @param is * @param os * @throws IOException */ public static void copy(InputStream is, OutputStream os) throws IOException { byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { os.write(bytes, 0, len); } if (os != null) os.close(); if (is != null) is.close(); }