Hi,
I have created pst file and adding messages to pst. I am getting casting exception can not cast to IGenericEnumerable
Exception in thread “main” java.lang.ClassCastException: mithi.files.pst.bulkadd.MapiMessageCollection cannot be cast to com.aspose.email.system.collections.generic.IGenericEnumerable
at mithi.files.pst.GeneratePSTApp.createMailArchive(GeneratePSTApp.java:115)
at mithi.export.GenerateMailArchive.generateFiles(GenerateMailArchive.java:73)
at mithi.export.GenerateMailArchive.generateFiles(GenerateMailArchive.java:73)
at mithi.export.ExportMailAsPSTApp.generateMailArchiveFiles(ExportMailAsPSTApp.java:19)
at mithi.export.ExportMailArchiveApp.execute(ExportMailArchiveApp.java:37)
at mithi.export.ExportMailArchiveApp.execute(ExportMailArchiveApp.java:37)
at mithi.apps.jobs.RestoreMailApp.execute(RestoreMailApp.java:43)
at mithi.apps.jobs.RestoreMailApp.execute(RestoreMailApp.java:43)
at mithi.apps.jobs.RestoreMailApp.main(RestoreMailApp.java:70)
My function is:
szFileName = “test.pst”;
pst = PersonalStorage.create(szFileName, FileFormatVersion.Unicode);
FolderInfo folder = pst.getRootFolder().addSubFolder(“testfolder”).getTextValue());
folder.addMessages((IGenericEnumerable) new MapiMessageCollection(arMsg)); //Message[] arMsg
And collection is as:
package mithi.files.pst.bulkadd;
import java.util.Iterator;
import javax.mail.Message;
import javax.mail.MessagingException;
import com.aspose.email.MailMessage;
import com.aspose.email.MapiMessage;
import com.sun.mail.imap.IMAPMessage;
// For complete examples and data files, please go to GitHub - aspose-email/Aspose.Email-for-Java: Aspose.Email for Java Examples
public class MapiMessageCollection implements Iterable {
private Message[] arMsgs;
public MapiMessageCollection(Message[] arMsgs) {
this.arMsgs = arMsgs;
}
public Iterator iterator() {
return new MapiMessageEnumerator(arMsgs);
}
public class MapiMessageEnumerator implements Iterator {
private Message[] msgs;
private int position = -1;
public MapiMessageEnumerator(Message[] oMsgs) {
this.msgs = oMsgs;
}
public boolean hasNext() {
position++;
return (position < this.msgs.length);
}
public void reset() {
position = -1;
}
public MapiMessage next() {
final IMAPMessage oImapMsg = (IMAPMessage)msgs[position];
// create mailmessage from msg and convert to MapiMessage
MailMessage oMailMsg1 = new MailMessage();
try {
MailMessage oMailMsg = MailMessage.load(oImapMsg.getMimeStream());
return MapiMessage.fromMailMessage(oMailMsg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return MapiMessage.fromMailMessage(oMailMsg1);
}
public void dispose() {
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
I wanted to add bulk messages to improve performance, it is taking too much time to create pst if mailstore size is greater than 200mb.