How to read MimeMessage from javax.mail.Folder

Hi
In order to apply the oracle fix:
http://www.oracle.com/technetwork/java/javamail/faq/index.html#imapserverbug
I have to change old code which reads all the mails via the Folder.getMessages()

The issue is, that Folder.getMessages() returns Message instead of MimeMessage
How can I fetch MimeMessages?
for the fix I require MimeMessages:
MimeMessage cmsg = new MimeMessage(msg);
msg must be of type MimeMessage


I open the folder:
private javax.mail.Folder openFolder(MailFolder folder, Person owner, Store store, boolean readWrite, boolean createFolder) throws MessagingException {
        Folder f;
        Folder defFolder = store.getDefaultFolder();
 ...
        f.open(readWrite ? Folder.READ_WRITE : Folder.READ_ONLY);
        return f;
    }
mruffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Look at the example code in the bug documentation you cite: they are doing more or less the same thing. The only difference is that they are using getMessage with an index number. The fact that you're using getMessages doesn't stop you casting to MimeMessage and using the copy constructor as they suggest as you iterate your Message[]

http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeMessage.html#MimeMessage(javax.mail.internet.MimeMessage)
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
In the link that you have provided, the (non-comment line) is this...
MimeMessage msg = (MimeMessage)folder.getMessage(n);

Open in new window

The cast gets you a MimeMessage object from the Message object that is returned from Folder.getMessage(). Did you try that?

Or are you saying that you did that but you got a ClassCastException when you ran the code?
0
mruffAuthor Commented:
Sorry, not precise enough from my side: Yes: Or are you saying that you did that but you got a ClassCastException when you ran the code?
I got a ClassCastException
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Sharon SethCommented:
Probably you are trying to cast an iterable to a MimeMessage?
0
CEHJCommented:
The first thing to do is

System.out.println(message[index].getClass())

Open in new window


to see what you have got
0
mruffAuthor Commented:
The folder.getMessages() returns an array of Messages[] I just do not kow how to fetch an array of MimeMessages instead, pls see also attachment.
getMessages.png
0
CEHJCommented:
That message is telling you something quite different: namely that your code needs to catch that exception
0
mruffAuthor Commented:
yes, this is the source code
What I want to know: How Can I read MimeMessages instead of Messages?
the code:
msgs = (MimeMessage[]) imapFolder.getMessages();
throws a ClassCastException
So How do I read MimeMessages?
0
CEHJCommented:
Please post the ClassCastException trace that you received

Also see (and act on) my comment HERE
0
Sharon SethCommented:
Do this instead :

Change    
msgs = (MimeMessage[]) imapFolder.getMessages();

Open in new window

to
 
msgs =  imapFolder.getMessages();

Open in new window


Now , when you iterate and pick up each item from msgs , cast that item to MimeMessage
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Now , when you iterate and pick up each item from msgs , cast that item to MimeMessage
Yes, i already mentioned that here (with emphasis)
doesn't stop you casting to MimeMessage and using the copy constructor as they suggest as you iterate your Message[]
0
CEHJCommented:
?
0
mruffAuthor Commented:
Hi,
Thx your solution worked:
doesn't stop you casting to MimeMessage and using the copy constructor as they suggest as you iterate your Message[]
0
Sharon SethCommented:
I didn't give that solution , CEHJ did
0
CEHJCommented:
You certainly helped sharonseth, but it would be nicer for me to not be completely passed over ;)
0
mruffAuthor Commented:
sorry, I fear I cannot reassign anymore?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.