0

Yesterday I asked on this site for an Exception that I didn't know how to handle, and I luckily found an answer in a very short time. Well.. here's another one. The class is the same of the other question: when I download messages from the imap server and handle them into an array, it gives me IMAPAddress Exception. This time I really don't know what I could do, I don't want to use POP3 because I just want to see emails stored on the server, not really handle them. Thank you for the attention.

Here's the code:

ScaricaEmail(String host,String porta,String user,String pw)
{
    this.host=host;
    this.porta=porta;
    nick=user;
    this.pw=pw;
}
public static Object[][] checkMail(String cartella) 
{
   Object[][] tabella;
   try
   {
        Properties propvals = new Properties();
        propvals.put("mail.imaps.host", host);
        propvals.put("mail.imaps.port", porta);
        propvals.put("mail.imaps.starttls.enable", "true");
        propvals.put("mail.imaps.ssl.trust", "*");
        Session emailSessionObj = Session.getDefaultInstance(propvals);  
        //Create IMAP store object and connect with the server
        Store storeObj = emailSessionObj.getStore("imaps");
        storeObj.connect(host, nick, pw);
        //Create folder object and open it in read-only mode
        Folder emailFolderObj = storeObj.getFolder(cartella);
        emailFolderObj.open(Folder.READ_ONLY);
        //Fetch messages from the folder and print in a loop
        Message[] messageobjs = emailFolderObj.getMessages(); 
        tabella=new Object[messageobjs.length][6];

        for(int i = 1; i <= messageobjs.length; i++)
        {
            Message m = messageobjs[i-1];
            String mimeType = m.getContentType();
            Object[] risultati=new String[6];
            risultati[i-1]=m.getFrom()[i-1];   //Here's where I get the Exception
            risultati[i-1]=m.getSubject();
            risultati[i-1]=getTestoDaMessaggio(m);
            risultati[i-1]=getContoAllegati(m);
            risultati[i-1]=m.getSentDate();
            risultati[i-1]=0;
            tabella[i-1]=risultati;
        } 

        emailFolderObj.close(false);
        storeObj.close();
      }
      catch (Exception exp)
      {
           exp.printStackTrace();
           tabella=null;
      }

      return tabella;
}

Here's the output:

java.lang.ArrayStoreException: com.sun.mail.imap.protocol.IMAPAddress
    at clientemail.ScaricaEmail.checkMail(ScaricaEmail.java:57)
    at clientemail.Home.initComponents(Home.java:240)
    at clientemail.Email$4.actionPerformed(Email.java:167)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)

Thank you.

1 Answers1

0
   Object[] risultati=new String[6];
   risultati[i-1]=m.getFrom()[i-1];   //Here's where I get the Exception

Message.getFrom() returns an Address[] and you are getting the first index which is an Address. Address is not a java.lang.String so it can't be stored in a String[].

To make it work you could do something like:

risultati[i-1]=String.valueOf(m.getFrom()[i-1]);

Or you can change the array type:

Object[] risultati=new Object[6];

In general, you avoid coping values to an array and just use the message object.

jmehrens
  • 10,580
  • 1
  • 38
  • 47