SpringIntegration Mail Failed to create new store connection Unrecognised SSL message
<p>I have a strange exception that happens since a week in a production environment, sporadically. Everything worked fine until now.</p>

<p>In the code I am connecting using imap to an MS Exchange server to read the emails. This part of the code is still working ok.</p>

<p>Once the email has been read it is archived in a subfolder. This is this part that is sometimes sending exception.</p>

<p>The exception is "Failed to create new store connection".</p>

<p>And just after this message I always have the following exception in the traces:</p>

<pre><code>javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at org.springframework.integration.mail.MailReceivingMessageSource.receive(MailReceivingMessageSource.java:117)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:144)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:192)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:292)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:726)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:194)
    at org.springframework.integration.mail.AbstractMailReceiver.connectStoreIfNecessary(AbstractMailReceiver.java:227)
    at org.springframework.integration.mail.AbstractMailReceiver.openFolder(AbstractMailReceiver.java:238)
    at org.springframework.integration.mail.AbstractMailReceiver.receive(AbstractMailReceiver.java:260)
    at org.springframework.integration.mail.MailReceivingMessageSource.receive(MailReceivingMessageSource.java:103)
</code></pre>

<p>What I think strange is that it is only when I connect to the store that this happens. I can still read the emails with no problems.</p>

<p>The exception is thrown in this piece of code:</p>

<pre><code>public void saveMessage(MimeMessage message, String folderName) {
    try {
        Folder folder = message.getFolder();
        folder.open(Folder.READ_WRITE);
        // Mark the message as Delete in its folder
        String messageId = message.getMessageID();
        Message[] messages = folder.getMessages();
</code></pre>

<p>It seems it is the getFolder that sends the message.</p>

<p>What I can't figure out is why do I have an SSL exception when I try to get access to the store, while the same uri is used without any issue to read the emails.</p>

<p>I have the following configuration:</p>

<pre><code>&lt;int:channel id="receiveChannel" datatype="javax.mail.internet.MimeMessage"/&gt;

&lt;int-mail:inbound-channel-adapter id="incomingEmailsAdapter"
    store-uri="${inboundMail.storeUri}"
    channel="receiveChannel"
    should-delete-messages="false"
    should-mark-messages-as-read="true"
    auto-startup="${inboundMail.startup}"
    java-mail-properties="javaMailInboundProperties"&gt;
    &lt;int:poller max-messages-per-poll="${inboundMail.nb.poll}" fixed-rate="${inboundMail.nb.rate}"/&gt;
&lt;/int-mail:inbound-channel-adapter&gt;

&lt;util:properties id="javaMailInboundProperties"&gt;
    &lt;prop key="mail.imap.socketFactory.class"&gt;javax.net.ssl.SSLSocketFactory&lt;/prop&gt;
    &lt;prop key="mail.imaps.ssl.trust"&gt;*&lt;/prop&gt;
    &lt;prop key="mail.imap.socketFactory.fallback"&gt;false&lt;/prop&gt;
    &lt;prop key="mail.store.protocol"&gt;imaps&lt;/prop&gt;
    &lt;prop key="mail.debug"&gt;${inboundMail.debug}&lt;/prop&gt;
&lt;/util:properties&gt;

&lt;int:service-activator id="serviceActivator" input-channel="receiveChannel" ref="mailService" method="handleMail"/&gt;
</code></pre>

<p>with:</p>

<p>inboundMail.storeUri = imaps://Unknown:REDACTED@company.com:993/inbox</p>

<p>Why can I read emails with this uri, and can I not open the store with the same one?</p>

<p>This issue happens several times a day, since a week.</p>

<p>Any idea where it could come from?</p>

<p>Thanks</p>

<p>Gilles</p>