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();
    }

 

posted on 2018-07-26 18:02  明湖樵夫  阅读(734)  评论(0编辑  收藏  举报