icon-arrow icon-check icon-mail icon-phone icon-facebook icon-linkedin icon-youtube icon-twitter icon-cheveron icon-download icon-instagram play close close icon-arrow-uturn icon-calendar icon-clock icon-search icon-chevron-process icon-skills icon-knowledge icon-kite icon-education icon-languages icon-tools icon-experience icon-coffee-cup
Werken bij Integration & Application Talents
Blog 24/03/2016

Poll a Microsoft Exchange mailbox from OSB using DavMail

Mail

Laurens van der Starre Integratie expert bij Integration & Application Talents
Laurens van der Starre /
Integratie expert

The Oracle Service Bus has built in features to poll an IMAP of POP3 mail account. There are loads of excellent blog posts that exactly describe how to do it. However, when the mail server happens to be a Microsoft Exchange server, things become shady. Exchange’s IMAP implementation tends to be not really that compatible, and the server log will fill up with stacktraces such as:

javax.mail.MessagingException: Connect failed;
  nested exception is:
        java.net.ConnectException: Connection timed out
javax.mail.MessagingException: Connect failed;
  nested exception is:
        java.net.ConnectException: Connection timed out
        at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161)
        at javax.mail.Service.connect(Service.java:288)
        at com.bea.wli.sb.transports.email.util.MailInbox.<init>(MailInbox.java:49)
        at com.bea.wli.sb.transports.email.EmailWorkPartitioningAgent.execute(EmailWorkPartitioningAgent.java:100)
        at com.bea.wli.sb.transports.poller.TransportTimerListener.run(TransportTimerListener.java:74)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        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:722)
Caused by: java.net.ConnectException: Connection timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)

Of course we can connect using the Exchange Web Services (EWS) using the OSB, but that is loads of work. Luckily there is DavMail. DavMail is an gateway that sits in between Exchange and your third party mail clients. Using DavMail, you can connect using simple POP3 or IMAP protocols to DavMail which in turn connects to Exchange using the EWS. DavMail is build in java, and runs happily on WebLogic Server.

So, download DavMail’s WAR distribution for SourceForge. Unpack the WAR-file and edit the properties file in WEB-INF/classes.

Set is to run in Server mode, and let it know where the EWS is:

 Server or workstation mode
davmail.server=true
# connection mode auto, EWS or WebDav
davmail.enableEws=true
# base Exchange OWA or EWS url
davmail.url=https://<<your exchange server>>/ews/Exchange.asmx

Define the locally used ports to which the OSB can connect (note that ports below 1024 require root on Linux):

# Listener ports
davmail.caldavPort=5280
davmail.imapPort=5143
davmail.ldapPort=5389
davmail.popPort=5110
davmail.smtpPort=5025

Allow connections to DavMail:

# allow remote connection to DavMail
davmail.allowRemote=true

I recommend exposing DavMail only to OSB, so let it bind to localhost:

# bind server sockets to a specific address
davmail.bindAddress=localhost

Don’t make it difficult for yourself, and make to local connections plain:

# disable SSL for specified listeners
davmail.ssl.nosecurecaldav=true
davmail.ssl.nosecureimap=true
davmail.ssl.nosecureldap=true
davmail.ssl.nosecurepop=true
davmail.ssl.nosecuresmtp=true

That should do it. Of course you can tinker with the other settings (like log file locations etc.).

If it happens to be the case that your Exchange server has self-signed SSL certificates, please make sure to add them to your trust store. You can of course hack these certificates into your cacerts keystore that is part of the JVM, or (better solution) add them to your managed server’s trust store. Please make sure the trust store is loaded by WebLogic: edit the <DOMAIN_HOME>/bin/setDomainEnv.sh and add:

-Djavax.net.ssl.trustStore=yourtuststore.jks -Djavax.net.ssl.trustStorePassword=...."

to EXTRA_JAVA_PROPERTIES.

Now pack the WAR-file using your favorite tool, and deploy it to your webLogic server as an application. You can now create an OSB proxy service that polls an Exchange server by connecting to your DavMail gateway on your local IMAP of POP3 ports.

One final note: if you happen to run into the following error while using IMAP:

javax.mail.MessageRemovedException
        at com.sun.mail.imap.IMAPMessage.checkExpunged(IMAPMessage.java:220)
        at com.sun.mail.imap.IMAPMessage.getRecipients(IMAPMessage.java:286)
        at com.bea.wli.sb.transports.email.EmailWorkPartitioningAgent.createPublishedTask(EmailWorkPartitioningAgent.java:381)
        at com.bea.wli.sb.transports.email.EmailWorkPartitioningAgent.processInboxMessages(EmailWorkPartitioningAgent.java:256)
        at com.bea.wli.sb.transports.email.EmailWorkPartitioningAgent.execute(EmailWorkPartitioningAgent.java:104)
        at com.bea.wli.sb.transports.poller.TransportTimerListener.run(TransportTimerListener.java:74)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        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:722)

then there seems to be some kind of threading issue with the Post Read Action defined in the OSB proxy. Spare yourself the trouble and switch to POP3. That’ll work.

Happy coding!

Geen reacties

Geef jouw mening

Reactie plaatsen

Reactie toevoegen

Jouw e-mailadres wordt niet openbaar gemaakt.

Geen HTML

  • Geen HTML toegestaan.
  • Regels en alinea's worden automatisch gesplitst.
  • Web- en e-mailadressen worden automatisch naar links omgezet.
Laurens van der Starre Integratie expert bij Integration & Application Talents
Laurens van der Starre /
Integratie expert

Wil je deel uitmaken van een groep gedreven en ambitieuze experts? Stuur ons jouw cv!