Microsoft Exchange Email on Linux

They make it hard, but not impossible

I dislike web clients. I couldn’t tell you precisely why, but, given the choice, I will always prefer a discrete desktop application to one that ones in a web browser. This is particularly true of email clients.

This can cause an issue though. Employers and Universities both are quite fond of using Microsoft Exchange or Office 365 as an email provider. And, because Microsoft is Microsoft, these services do not support the standard IMAP/POP/SMTP interfaces, which makes getting them set up on Linux a royal pain.

That is not, however, to say that it is impossible. There are several different ways to get your Microsoft email account set up on a Linux desktop email client. I’m going to address two of those ways here, starting with the simplest one.

Evolution EWS

By far the easiest way to go about this is to use the evolution-ews plugin to the Evolution email client. Ordinarily, setting up an account in Evolution will make it available to other Gnome applications that use the same back-end. In fact, this is the only way to get CalDav and CardDav accounts working in Gnome’s default Calendar and Contacts applications (I go over how to do this in this video), which support the protocol but have no way to configure it themselves.

Unfortunately, this is not the case with evolution-ews, or at least it doesn’t seem to be. I’ve not gotten it working! This means that, if you go this route, you’re stuck with Evolution as your email client. Which may be fine, Evolution is by no means a bad peice of software. But if you’re interested in using a different client, skip ahead to the next section of this article.

First, you need to install the plugin. On Ubuntu based systems, that is done with

$ sudo apt-get install evolution-ews

Once this is done, launch Evolution and go to Edit->Accounts->Add->Mail Account. Then click next, enter your name and email address into the dialog, uncheck “Look up mail server details…” and click next again. On the next screen, you need to select “Exchange Web Services” under the Server Type drop-down menu. This won’t appear as an option until the evolution-ews package has been installed.

The host URL should be auto-populated with the “default” URL for your domain, based on your email address. This URL may or may not work, depending on how your organization has configured their Exchange setup. You may as well give it a shot–click the “Fetch URL” button. It may prompt you for a password (potentially several times in a row, in my experience). If it works, the OAB URL field should be filled in.

More often than not, though, the default Host URL will not work. If this is the case, try using this one: https://outlook.office365.com/EWS/Exchange.asmx

Once the OAB URL has been filled in, click through the rest of the settings, and you should be good to go!

A more “Linuxy” Solution: DavMail

The above solution works, so long as you’re happy with Evolution. But if you want to use some other email client, you’ll need to cut a different path. Luckily, there is a program called DavMail that will allow us to do just that.

Rather than connecting our email client to Exchange, what we’ll do instead is connect to Exchange using DavMail. We can then connect any email client of our choosing to DavMail, which will expose a standard IMAP interface to the connected Exchange server. It’s a very cool process indeed.

First, you’ll need to install DavMail. For Debian-based systems it’s as simple as,

$ sudo apt-get install davmail

There are two ways to configure DavMail. It have a graphic interface that you can use, or you can set up a configuration file. If you want to go the graphical route, check out the official DavMail documentation page. Otherwise, stick around and I’ll run through the process of using a configuration file, which I find both faster and more convenient.

If you want to go the configuration file route, note that the DavMail command will accept a configuration file as its first argument. You can use this if you want to set up multiple different Exchange accounts on the same computer.

If you’re like me, and only have one, then for convenience I like to add the following alias to my shell’s configuration,

alias davmail="davmail \"$HOME/.config/davmail/davmail.properties\""

and then place the configuration in ~/.config/davmail/davmail.properties.

As far as the configuration file goes, here is what mine looks like,

# Disallow access to the davmail server from remote hosts (i.e., other
# computers on the network)
davmail.allowRemote=false

# Don't use SSL--more on this below
davmail.ssl.nosecurecaldav=false
davmail.ssl.nosecureimap=false
davmail.ssl.nosecureldap=false
davmail.ssl.nosecuresmtp=false

# Ports to run the different services on. You'll need these to connect
# your clients. If you have several Exchange accounts, each one will need
# to run on different ports
davmail.caldavPort=5000
davmail.imapPort=5001
davmail.ldapPort=5002
davmail.smtpPort=5003

# Connection details for your exchange account. Odds are good that the
# url listed here will work for you. If not, see if your University/employer
# has any details on the correct host URL to connect to their email services
# with.
davmail.url=https://outlook.office365.com/EWS/Exchange.asmx

# Run davmail in server mode
davmail.server=true
davmail.enableKeepAlive=true

This is a fairly minimal configuration that works for me. It’s possible you may need a few more configuration options to get things working properly for you. Check out the DavMail server documentation for more details on configuring it.

Note that I am not using SSL for my connection, which means that clients connecting to the DavMail server will not use encryption. This does not mean that my connections to the Exchange server are not encrypted–these still are. It is only the connection between my email client and the DavMail server that are in the clear. This diagram demonstrates what I mean,

Because none of the unencrypted traffic actually hits the network, running SSL over this connection isn’t strictly necessary. However, if you want to allow external connections to your DavMail server (maybe run one instance of it on your network for all your devices to connect to, etc.), or if you want to enable SSL out of an abundance of caution, you can check out the DavMail documentation on setting up SSL.

Once the configuration file is ready, run the server. For testing purposes, just run the command directly,

$ davmail

Now, you should see some basic diagnostic information displayed on the terminal. The server is now running, so next we need to connect to it with an email client of your choice. Leave the terminal running DavMail open. If you close it, you’ll shut down the server and be unable to connect to it.

This will be done just like setting up any other IMAP/SMTP connection, except that you want to use localhost or 127.0.0.1 as the IMAP and SMTP urls, and use the ports specified in the configuration file (5001 for IMAP and 5003 for SMTP in the example here). Be sure to use your full email address as your username.

Unfortunately, this setup will require that you use an email client that allows you to specify the ports to connect to for IMAP and SMTP, which rules out Gnome Online Accounts and Geary, to name just a few, unless you want to run DavMail with the ports set to their appropriate default values (which will require running it as root).

Once you connect successfully and verify that it works, it would be ideal if you didn’t have to run DavMail manually and leave a terminal open constantly to access your email. There are actually any number of ways to accomplish this task.

The absolute simplest, manual way is to simply run,

$ nohup davmail &

which will run it in the background, and allow you to close the terminal without killing the server.

You might get the idea of putting this command in your bashrc file or something to automatically run it, but this would be a bad idea. You only want to run DavMail once with a given configuration file. If you try to run multiple instances, they duplicates will crash because they cannot bind to the specified ports.

There are, however, other ways to automatically run a command like this. Your desktop environment may have a way to configure startup jobs (like the Startup Applications Preferences window in Gnome). Or you could create a systemd unit and then use systemd to automatically run/manage DavMail.

Conclusion

Whichever way you did it, hopefully your were able to get everything connected up and working. Microsoft does not make it easy, but it is usually doable. In a future installment, I’ll walk through how I integrate davmail into my own email setup.

Related