qq_32354577 2018-12-19 17:30 采纳率: 0%
浏览 570

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

图片说明

  • 写回答

1条回答

  • 钢弹~ 2018-12-19 09:41
    关注

    你这个正文是个流 (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();
                }
            }
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波