Mail From Exchange Server on Ubuntu 12.04 With Thunderbird

October 2nd, 2013 Permalink

Daring corporate rebels are often found running Ubuntu in a Microsoft environment, leaving them with the need to find a way to connect to an Exchange Server instance for mail, calendar, and address book. To make life more interesting the Exchange Server may not offer IMAP, POP3, or any other open email protocol. That might be seen as a challenge, and so in this post you'll find a recipe for using Thunderbird with Exchange Server that was tested with Ubuntu 12.04 and Exchange Server 2007, but which should still work for more modern Exchange Server variants.

Farewell ExQuilla, We Hardly Knew Ye

ExQuilla is a plugin for Thunderbird that connects to Exchange Server using Exchange Web Services (EWS). It is a quality piece of work, but is going professional and will be a paid product from late 2013 on. You might investigate using it in place of this recipe, as it should be much easier to set up.

Install Java

But on with the recipe, which starts with a Java installation, as so many things do:

sudo apt-get install default-jdk

Install DavMail

DavMail is a proxy server that will allow you to connect to Exchange Server using IMAP or POP3 protocols, while under the hood EWS is used. To install it follow the DavMail Linux setup instructions to obtain a .deb package. Before installing it, you should run this command, per the instructions at the DavMail site.

gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"

To install from the command line (adjust the version to whatever the latest happens to be when you do this):

sudo dpkg -i davmail_4.3.4-2174-1_all.deb

Don't run DavMail after installation. Just quit if it launches automatically and unlock it from the launcher. You may have to chase down Java processes running DavMail and kill them, depending on circumstances.

Set Up DavMail As a Service

You will need to set up DavMail to run quietly as a service. So create an upstart configuration file at /etc/init/davmail.conf:

#
# DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
#
description "DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway"

start on started network-manager
stop on stopped network-manager

env DAVEXEC=/usr/bin/davmail
env DAVCONF=/etc/davmail/davmail.properties
env DAVLOG=/var/log/davmail.log

expect fork
respawn

exec /usr/bin/nohup $DAVEXEC $DAVCONF 2>>$DAVLOG >>$DAVLOG &

Then create the configuration file /etc/davmail/davmail.properties:

# DavMail settings, see http://davmail.sourceforge.net/ for documentation

#############################################################
# Basic settings

# Server or workstation mode
davmail.server=true
# connection mode auto, EWS or WebDav
davmail.enableEws=auto
# base Exchange OWA or EWS url
davmail.url=https://exchange.example.com/owa/
# Listener ports
davmail.caldavPort=1080
davmail.imapPort=1143
davmail.ldapPort=1389
davmail.popPort=1110
davmail.smtpPort=1025

#############################################################
# Network settings

# Network proxy settings
davmail.enableProxy=false
davmail.useSystemProxies=false
davmail.proxyHost=
davmail.proxyPort=
davmail.proxyUser=
davmail.proxyPassword=
# proxy exclude list
davmail.noProxyFor=
# allow remote connection to DavMail
davmail.allowRemote=false
# bind server sockets to a specific address
davmail.bindAddress=
# client connections SO timeout in seconds
davmail.clientSoTimeout=
# DavMail listeners SSL configuration
davmail.ssl.keystoreType=
davmail.ssl.keystoreFile=
davmail.ssl.keystorePass=
davmail.ssl.keyPass=
# Accept specified certificate even if invalid according to trust store
davmail.server.certificate.hash=
# disable SSL for specified listeners
davmail.ssl.nosecurecaldav=false
davmail.ssl.nosecureimap=false
davmail.ssl.nosecureldap=false
davmail.ssl.nosecurepop=false
davmail.ssl.nosecuresmtp=false
# disable update check
davmail.disableUpdateCheck=true
# Send keepalive character during large folder and messages download
davmail.enableKeepalive=false

#############################################################
# Caldav settings

# override default alarm sound
davmail.caldavAlarmSound=
# retrieve calendar events not older than 90 days
davmail.caldavPastDelay=90
# WebDav only: force event update to trigger ActiveSync clients update
davmail.forceActiveSyncUpdate=false

#############################################################
# IMAP settings

# Delete messages immediately on IMAP STORE Deleted flag
davmail.imapAutoExpunge=true
# Enable IDLE support, set polling delay in minutes
davmail.imapIdleDelay=

#############################################################
# POP settings

# Delete messages on server after 30 days
davmail.keepDelay=30
# Delete messages in server sent folder after 90 days
davmail.sentKeepDelay=90
# Mark retrieved messages read on server
davmail.popMarkReadOnRetr=false

#############################################################
# SMTP settings

# let Exchange save a copy of sent messages in Sent folder
davmail.smtpSaveInSent=true

#############################################################
# Loggings settings

# log file path, leave empty for default path
davmail.logFilePath=/var/log/davmail.log
# maximum log file size, use Log4J syntax, set to 0 to use an external
# rotation mechanism, e.g. logrotate
davmail.logFileSize=0
# log levels
log4j.logger.davmail=WARN
log4j.logger.httpclient.wire=WARN
log4j.logger.org.apache.commons.httpclient=WARN
log4j.rootLogger=WARN

#############################################################
# Workstation only settings

# smartcard access settings
davmail.ssl.pkcs11Config=
davmail.ssl.pkcs11Library=
# SSL settings for mutual authentication
davmail.ssl.clientKeystoreType=
davmail.ssl.clientKeystoreFile=
davmail.ssl.clientKeystorePass=
# disable all balloon notifications
davmail.disableGuiNotifications=false
# disable startup balloon notifications
davmail.showStartupBanner=true
# enable transparent client Kerberos authentication
davmail.enableKerberos=false

The only setting you absolutely have to change is davmail.url, the Exchange Server URL. The rest should work fine as-is, but of course you are free to tinker.

Other DavMail Configuration

You may have to set up the following symlinks for this to work as a service. If the relevant .swt/* folders don't exist under root, then create them:

sudo ln -s /usr/lib/jni/libswt-* /root/.swt/lib/linux/x86_64/

You should also set up log rotation. Add the instructions to rotate the DavMail log file in /etc/logrotate.d/davmail:

/var/log/davmail.log
{
        rotate 7
        daily
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                invoke-rc.d rsyslog reload >/dev/null 2>&1 || true
        endscript
}

Finally start the service:

service davmail start

Check the log for errors and use nmap or similar to make sure that DavMail is binding to the ports it should be.

Configure Your Account in Thunderbird

Fire up Thunderbird and set up a mail account in Thunderbird normally, but select "Manual config" to establish an IMAP account with the following properties:

  • Incoming Server: localhost
  • Port: 1143
  • SSL: None
  • Authentication: Normal password
  • OutgoingServer: localhost
  • Port: 1025
  • SSL: None
  • Authentication: Normal password

You can choose to set up a POP3 account and it will work, but you will probably come to regret it in the not so distant future. The effects of using POP3 with DavMail and Exchange Server are sometimes unpredictable: expect duplicated email, confusion about whether messages are still on the server or not, and other, similar entertainments.

Setting Up the Address Book

Open the address book tab in Thunderbird, and add a new LDAP-based address book, under File -> New -> LDAP Directory. Then fill out the properties as follows:

  • Name: My Address Book
  • Hostname: localhost
  • Base DN: ou=people
  • Port number: 1389
  • Bind DN: WINDOWS_DOMAINusername

In the Properties dialog for the new address book there is an Offline tab, and there you'll see a download button. It is probably a good idea to use this to pull down the address book in order to speed up local searches - also a good way to test to see that your entered configuration is correct. Note that you won't see addresses listed in the address book pane for an LDAP-based address book until you actually search for them.

Calendar Integration

Open up the Add-ons tab in Thunderbird and search for "Lightning". Install the Lightning calendar extension, a quick and easy process. Without adding any additional extensions, you should now be able to subscribe to the calendar associated with your Exchange Server account via CalDav.

  • Open the calendar tab in Thunderbird.
  • Add a new calendar via File -> New -> Calendar.
  • In the dialog, choose "On the Network".
Set the calendar location as follows, but replace "username@example.com" with the email address associate with your Exchange Server account:
http://localhost:1080/users/username@example.com/calendar

Lastly give the calendar a name and finish up the creation dialog with the default settings. This should start to pull down calendar events immediately, though you may have to restart Thunderbird to see the new calendar in the calendar tab.