java.lang.ClassCastException: javax.mail.internet.MimeMultipart 无法在 NewClass.main(NewClass.java:34) 处转换​​为 java.lang.String

发布于 2024-11-26 08:19:25 字数 2218 浏览 2 评论 0原文

这是用于从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-12-03 08:19:25

错误在于

JEditorPane ep = new JEditorPane("text/html" , (String)msgs[i].getContent());

您的多部分消息 msgs[i].getContent() 返回 javax.mail.internet.MimeMultipart。您可以对其调用 toString,但正确的方法是从中获取邮件部分。首先,您可以通过 instanceof MimeMultipart 进行检查。查看 JAVAMAIL API 常见问题解答如何处理多部分消息。

The error is here

JEditorPane ep = new JEditorPane("text/html" , (String)msgs[i].getContent());

you have multipart message msgs[i].getContent() returns javax.mail.internet.MimeMultipart. You can invoke toString on it but correct approach is getting mail parts from it. First you can check by instanceof MimeMultipart. Look at JAVAMAIL API FAQ how to deal with multipart messages.

鹿港小镇 2024-12-03 08:19:25

尝试将 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(). Try msgs[i].getContent().toString().

扬花落满肩 2024-12-03 08:19:25
(String)msgs[i].getContent()

这可能会返回一个 MimeMultiPart 对象,并且您将其转换为 String。

(String)msgs[i].getContent()

This perhaps returns a MimeMultiPart object and you are casting it to String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文