java.lang.ClassCastException: javax.mail.internet.MimeMultipart 无法在 NewClass.main(NewClass.java:34) 处转换为 java.lang.String
这是用于从 Gmail 服务器获取电子邮件的代码。除此之外,还将主题和发件人分开。我正在检查的收件箱有 5 条消息。(一些已读,一些未读) 我希望 html 内容可见,所以我使用了 JEditorPane
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.swing.*;
class NewClass {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.imap.host" , "imap.gmail.com" );
props.put("mail.imap.user" , "username");
// User SSL
props.put("mail.imap.socketFactory" , 993);
props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.imap.port" , 993 );
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username" , "password");
}
});
try {
Store store = session.getStore("imap");
store.connect("imap.gmail.com" , "username" , "password");
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.READ_WRITE);
Message msgs[] = fldr.getMessages();
for(int i = 0 ; i < msgs.length ; i++) {
// program breaks after the following statement
System.out.println(InternetAddress.toString(msgs[i].getFrom()) + "<-- FROM" + " " + msgs[i].getSubject() + "<---Subject");
JFrame fr = new JFrame();
JPanel p = new JPanel();
JEditorPane ep = new JEditorPane("text/html" , (String)msgs[i].getContent());
ep.setEditable(false);
JScrollPane sp = new JScrollPane(ep);
p.add(ep);
fr.add(p);
fr.setSize(300,300);
fr.setVisible(true);
}
} catch(Exception exc) {
}
}
}
我得到的输出是: Gmail 团队 [电子邮件受保护]><- - 从获取手机上的 Gmail<---Subject
在此输出之后,程序给出以下异常 java.lang.ClassCastException: javax.mail.internet.MimeMultipart 无法转换为 java.lang.String 在 NewClass.main(NewClass.java:34)
处。 为什么框架不可见?
This is the code that is intended to fetch up the email from Gmail server. Along with it also brings the subject and the sender separately. The inbox that i am checking has 5 messages.(some read and some unread)
I wanted the html content to be visible , so i used JEditorPane
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.swing.*;
class NewClass {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.imap.host" , "imap.gmail.com" );
props.put("mail.imap.user" , "username");
// User SSL
props.put("mail.imap.socketFactory" , 993);
props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.imap.port" , 993 );
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username" , "password");
}
});
try {
Store store = session.getStore("imap");
store.connect("imap.gmail.com" , "username" , "password");
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.READ_WRITE);
Message msgs[] = fldr.getMessages();
for(int i = 0 ; i < msgs.length ; i++) {
// program breaks after the following statement
System.out.println(InternetAddress.toString(msgs[i].getFrom()) + "<-- FROM" + " " + msgs[i].getSubject() + "<---Subject");
JFrame fr = new JFrame();
JPanel p = new JPanel();
JEditorPane ep = new JEditorPane("text/html" , (String)msgs[i].getContent());
ep.setEditable(false);
JScrollPane sp = new JScrollPane(ep);
p.add(ep);
fr.add(p);
fr.setSize(300,300);
fr.setVisible(true);
}
} catch(Exception exc) {
}
}
}
The output that i get is :Gmail Team <[email protected]><-- FROM Get Gmail on your mobile phone<---Subject
After this output the program gives the following exception java.lang.ClassCastException: javax.mail.internet.MimeMultipart cannot be cast to java.lang.String
.
at NewClass.main(NewClass.java:34)
Why the frame is not visible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误在于
您的多部分消息
msgs[i].getContent()
返回javax.mail.internet.MimeMultipart
。您可以对其调用 toString,但正确的方法是从中获取邮件部分。首先,您可以通过instanceof MimeMultipart
进行检查。查看 JAVAMAIL API 常见问题解答如何处理多部分消息。The error is here
you have multipart message
msgs[i].getContent()
returnsjavax.mail.internet.MimeMultipart
. You can invoke toString on it but correct approach is getting mail parts from it. First you can check byinstanceof MimeMultipart
. Look at JAVAMAIL API FAQ how to deal with multipart messages.尝试将
exc.printStackTrace()
放入 catch 块中以查看问题所在。/e
您的问题是
(String)msgs[i].getContent()
。尝试msgs[i].getContent().toString()
。Try putting
exc.printStackTrace()
in your catch block to see what the problem is./e
Your problem is
(String)msgs[i].getContent()
. Trymsgs[i].getContent().toString()
.这可能会返回一个 MimeMultiPart 对象,并且您将其转换为 String。
This perhaps returns a MimeMultiPart object and you are casting it to String.