Search code examples
emailspring-integrationexchange-serverimap

How to retrieve email content body from "text/html; charset=us-ascii" with Spring Integration Mail


I am using Spring Integration Mail to pull and process email's content from Exchange Server. using imap-idle-channel-adapter. Email are being pulled fine. If email has attachments, the content type is multipart/mixed ("text/html; charset=us-ascii" and attachment) and email body from text/html; charset=us-ascii is retrieved fine.

However, if email doesn't have attachments the content type is just "text/html; charset=us-ascii". Sometimes, it comes as "text/html charset=windows-1252". In any case, even though the content object is String, the email body is retrieved as null or empty.

How should I retrieve the email body? If email is multipart/mixed, the email body for text/html is retrieved fine. So weird!

Code

enter image description here

Console

enter image description here

The configuration is like this.

<int-mail:imap-idle-channel-adapter id="customAdapter"
  store-uri="imaps://[username]:[password]@[exchange-imap-server]:993/INBOX"
  channel="receiveChannel"
  auto-startup="true"
  should-delete-messages="false"
  should-mark-messages-as-read="true"
  auto-close-folder="false"
  java-mail-properties="javaMailProperties"/>

<util:properties id="javaMailProperties">
  <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
  <prop key="mail.imap.socketFactory.fallback">false</prop>
  <prop key="mail.store.protocol">imaps</prop>
  <prop key="mail.debug">false</prop>
</util:properties>

 

As I said, email body cannot be retrieved only when email comes without attachment. The content type is "text/html; charset=us-ascii".

If the content is "multipart/mixed", the email body content can be retrieved fine even though the BodyPart for email body has the same content type "text/html; charset=us-ascii".

Same code retrieve in all cases when email are pulled from gmail.


Solution

  • Finally, I was able to retrieve the content text when email comes without attachments. The content type is "text/html; charset=us-ascii". I tested the fix in Outlook (Exchange Server) and Gmail. The fix is adding in the imap-idle-channel-adapter

    simple-content=true

    The payload will be javax.mail.Message, instead of IMAPMessage.