Pierpaolo Piccoli
Pierpaolo Piccoli

Reputation: 145

org.springframework.integration.mail.class cast exception changing flag on a mailmessage received with spring integration mail - imap

I want to update the flag on some message on an remote imap mail folder. I'm using

To achieve this goal i'm using an mail inbound channel adapter:

<mail:inbound-channel-adapter
    id="mailsGiaLetteAdapter"
    store-uri="imaps://${maildispatcher.daemon.legalmail.user}:${maildispatcher.daemon.legalmail.password}@${maildispatcher.daemon.legalmail.imap.host}:${maildispatcher.daemon.legalmail.imap.port}/INBOX" 
    channel="emailsGiaLetteChannel" 
    auto-startup="true" 
    should-delete-messages="false"   
    should-mark-messages-as-read="true" 
    java-mail-properties="javaMailProperties" 
    search-term-strategy="mailGiaLetteSearchTermStrategy"
    >
        <int:poller  fixed-rate="5000" max-messages-per-poll="1000" >
            <int:transactional  synchronization-factory="syncFactoryImpostazioneDelleMailComeDaLeggere" />
    </int:poller>

 </mail:inbound-channel-adapter>

I succeffuly obtain the message filtered by searchTermStrategy.

I obtain a collection of

javax.mail.internet.MimeMessage

This is an iterface.

The real implementations are instances of

  IntegrationMimeMessage

a private class defined in

org.springframework.integration.mail.AbstractMailReceiver

i've tried to update java mail library to the very last.

To change the mail flag Seen from true to false i do the following:

 // message is a MimeMessage implemented with IntegrationMimeMessage
 Folder folder = message.getFolder();
 folder.open(Folder.READ_WRITE);
 MimeMessage[] messages = {message};
        Flags flags = new Flags(Flag.SEEN);
        folder.setFlags(messages,flags , false);

I receive a ClassCastException.

  org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage cannot be cast to com.sun.mail.imap.IMAPMessage

in javamail method Utility.toMessageSet:

   org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage cannot be cast to com.sun.mail.imap.IMAPMessage
at com.sun.mail.imap.Utility.toMessageSet(Utility.java:61)
at com.sun.mail.imap.Utility.toMessageSetSorted(Utility.java:138)
at com.sun.mail.imap.IMAPFolder.setFlags(IMAPFolder.java:1415)
at it.assimoco.maildispatcher.daemon.legalmail.ProcessorePerImpostareLeMailComeDaLeggere.afterCommit(ProcessorePerImpostareLeMailComeDaLeggere.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

particularly in method Utility.toMessageSet this cast causes the error:

   java.mail.Message msg = (IMAPMessage)msgs[i];

The problem is that

but IntegrationMimeMessage and ImapMimeMessage are unrelated so the cause of the error.

Upvotes: 0

Views: 864

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

Looking at the source code of IntegrationMimeMessage in AbstractMailReceiver, there doesn't seem to be a way to do this. IntegrationMimeMessage is wrapping the original MimeMessage, but without exposing enough information to get back to the original. There is a getFolder method that should allow you to access the original Folder object, and the comment claims it exists for this purpose, but without the original IMAPMessage object or the original message number, you can't use the setFlags method on the Folder object.

You should probably file an issue against spring-integration.

Upvotes: 1

Related Questions