java.lang.ClassCastException:java.lang.String不能转换为javax.mail.Multipart [英] java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
本文介绍了java.lang.ClassCastException:java.lang.String不能转换为javax.mail.Multipart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我从java教程中获取的代码,但是当我尝试接收从计算机发送的正常消息,而不是通过GMail发送时,我的问题出现了。如果我通过GMail收到电子邮件,它运行正常并返回邮件,但是尝试从常规桌面邮件客户端检索邮件返回
This is my code below taken from a java tutorial - however my issue comes in when I try and recieve a normal message sent from a computer, as opposed to sent through GMail. If I recieve the email through GMail it runs fine and returns the mail, however trying to retrieve a mail from a conventional desktop mail client returns
错误:
java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
at gridnotifierproject_pcbuild.HandleMailInput.retrieveOneMail(HandleMailInput.java:37)
at gridnotifierproject_pcbuild.GridNotifierProject_PCBuild.main(GridNotifierProject_PCBuild.java:22)
代码:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "***********@gmail.com", "******");
System.out.println("Established Connection to Server!");
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
System.out.println("Found specified Folder, retrieving the latest message...");
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
} catch (Exception mex) {
mex.printStackTrace();
}
推荐答案
!
您的消息内容返回String,您正在尝试键入转换为Multipart。
Your message content returning String and you are trying to type cast to Multipart.
Object content = msg.getContent();
if (content instanceof String)
{
String body = (String)content;
...
}
else if (content instanceof Multipart)
{
Multipart mp = (Multipart)content;
...
}
这篇关于java.lang.ClassCastException:java.lang.String不能转换为javax.mail.Multipart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文