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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序