javamail读取正文的问题

根据网上代码,正文无法正确读取请问有人遇到过类似问题吗?

try {
                 Properties props = new Properties();
                 Session session = Session.getDefaultInstance(props);
                 Store store = session.getStore("pop3");
                 store.connect("pop.163.com", "xxx@163.com", "xxxx");
                 Folder folder = store.getFolder("INBOX");
                 folder.open(Folder.READ_ONLY);             
                 Message message[] = folder.getMessages(); 
                 System.out.println(message[0].getContentType());   
                 System.out.println(message[0].getContent().toString());
                 folder.close(true);
                 store.close();
            }

图片说明

你这个正文是个流 (Stream),你直接调用toString方法得到的是对象的引用地址,你想得到字符串你得走正常的流读取的流程。

如以下代码:

// 1.定义目标文件
        File srcFile = new File("E:/Temp/Test1.txt");
        // 2.创建一个流,指向目标文件
        InputStream is = null;
        try {
            is = new FileInputStream(srcFile);
            //3.创建一个用来存储读取数据的缓冲数组
            byte[]array = new byte[128];
            //4.循环往外流(count为每次读取数组中的有效字节总数)
            int count = is.read(array);
            // 5.循环打印
            while (count != -1) {
                // 将byte[] -》 String
                // 将byte数组读取到的有效字节转换成字符串
                String string = new String(array, 0, count);
                System.out.print(string);
                count = is.read(array);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭io流
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }