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

只愿长相守 提交于 2021-01-29 14:44:33

问题


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

  • spring-integration-mail 4.2.6.RELEASE
  • javamail com.sun.mail

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

  • IntegrationMimeMessage that arrives as parameter to toMessageSet method extends MimeMessage
  • ImapMessage extends MimeMessage

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


回答1:


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.



来源:https://stackoverflow.com/questions/58029087/org-springframework-integration-mail-class-cast-exception-changing-flag-on-a-mai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!