Suhail Gupta
Suhail Gupta

Reputation: 23216

java.lang.ClassCastException: javax.mail.internet.MimeMultipart cannot be cast to java.lang.String at 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 ?

Upvotes: 1

Views: 10659

Answers (3)

zacheusz
zacheusz

Reputation: 8842

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.

Upvotes: 4

Kumar Bibek
Kumar Bibek

Reputation: 9117

(String)msgs[i].getContent()

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

Upvotes: 1

Jeffrey
Jeffrey

Reputation: 44808

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().

Upvotes: 1

Related Questions