无法使用JavaMail读取邮件内容 [英] Not able to read content of mail using JavaMail

查看:466
本文介绍了无法使用JavaMail读取邮件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaMail

Object msg = message.getContent();
Multipart mp = (Multipart) msg;
for(int k = 0; k < mp.getCount(); k++) {
    BodyPart bp = mp.getBodyPart(k);
    if (bp.isMimeType("text/plain")) {
        String s = (String) bp.getContent();
        System.out.println("Content:" + s);
    }
    else if(bp.isMimeType("text/html")) {
        String s = (String) bp.getContent();
        System.out.println("Content:" + s);
    }
}

但是我遇到了以下错误:

But I'm getting the following error:

java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart

我如何删除它?

推荐答案

返回对象的类型取决于内容本身.返回text/plain内容的对象通常是String对象.返回multipart内容的对象始终是Multipart子类.

The type of the returned object is dependent on the content itself. The object returned for text/plain content is usually a String object. The object returned for a multipart content is always a Multipart subclass.

使用perator instanceof找出对象的类别.

Use perator instanceof, to find out what class of the object.

Object content = message.getContent();  
if(content instanceof String) {  
   ...
} else if(content instanceof Multipart) {  
    ...  
}  

这篇关于无法使用JavaMail读取邮件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆