Microsoft Exchange Email on Linux

They make it hard, but not impossible

Employers and universities are fond of using Microsoft Exchange/Office 365/Outlook as an email provider. And, because Microsoft is Microsoft, these services do not support standard IMAP/POP/SMTP interfaces. This makes getting them set up on Linux a royal pain (unless you’re content to use the Web interface, anyway).

That is not to say that it is impossible. There are several different ways to connect to your Microsoft email account using a Linux desktop email client. In this article, I discuss two of them.

Evolution EWS

By far the easiest way to go is to use the evolution-ews plugin to the Evolution email client. This approach is quick and easy, but requires that you use Evolution.1 If you’d rather use a different email client, skip ahead to the next part. Otherwise, read on!

The plugin itself isn’t usually packaged with Evolution itself, so you’ll first need to install the standalone package. On Ubuntu the command for this is,

$ sudo apt-get install evolution-ews

Once the plugin is installed, 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. “Exchange Web Services” won’t appear as an option until the evolution-ews package has been installed.

The host URL will be auto-populated with a “default” URL for your domain, based on your email address, which 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 (several times in a row, in my experience). If it works, the OAB URL field should be populated.

More often than not, though, the default 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 populated, click through the rest of the settings, and you should be good to go!

A more “Linuxy” Solution: DavMail

Update (12/24/22): In September of this year, Microsoft disabled Basic Authentication, which was the default authentication method used by DavMail. DavMail still works, but it requires a few extra steps to authenticate than were originally included in this article. I’ve added an update to this article at the relevant point describing the new process.

If you’re happy with Evolution, then the plugin above should be all you need. But, if you’d like to use any other email client, then you’ll need a different approach. Luckily, there is a program called DavMail that will allow us to access Microsoft email from a wide variety of different email clients.

DavMail is a standalone server that converts Microsoft’s proprietary email protocols into standard IMAP and SMTP. So, rather than connecting our email client to Exchange directly, we will use DavMail as an intermediary. Our email client will connect to DavMail over IMAP/SMTP interfaces, and then DavMail will convert our email client’s requests into the MicroSpeak (tm) that Exchange understands.

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

$ sudo apt-get install davmail

If DavMail is not in your distribution’s repositories, you can download it directly from their site (it requires Java).

DavMail has a graphical interface that you can use to configure it, but I find writing a configuration file manually to be more convenient. If you want to go the graphical route, check out the official DavMail documentation. Otherwise, stick around and I’ll run through the process of writing a configuration file.

DavMail accepts the pathname of a configuration file as an argument, so to keep things organized, I like to place my DavMail configuration file in

~/.confg/davmail/davmail.properties

and then execute DavMail using,

$ davmail "$HOME/.config/davmail/davmail.properties"

As far as the configuration file itself, here’s what I use,

# 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.

Update (12/24/22): This configuration uses basic authentication, which no longer functions. In order to authenticate properly, add this line to your configuration file:

davmail.mode=O365Manual

This DavMail configuration does not use SSL, which means that clients connecting to the DavMail server will not use encryption. This does not mean that the connection to the Exchange server is not encrypted. It is only the connection between my email client and the DavMail server that are in the clear.

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, it’s easiest to run DavMail directly in a terminal,

$ davmail ~/.config/davmail/davmail.properties

You should see some basic diagnostic information displayed in 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.

Configuring your email client is done the same way as setting up any other IMAP/SMTP account, except that you want to use localhost or 127.0.0.1 for 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. Make sure that you do not select any options for encryption in your email client, unless you’ve configured DavMail for SSL.

Update (12/24/22): If using manual authentication, you will need to take an extra step during the initial connection of your client to DavMail. When you connect, DavMail will produce a URL in its terminal output. The URL will take you to a Microsoft login page. Sign into this, after which you will see a blank white page. Copy the URL of this blank page and paste it back into the DavMail prompt. After this, your email client should be able to sign in properly. For more details on this process, see this article. You may need to repeat this manual login process every couple of weeks–if your email sync begins to fail randomly, check DavMail’s output and see if it’s giving you another URL.

This configuration 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).

Automatically running DavMail

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.

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 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.

I personally run it on my Linux systems as a user unit in systemd, with the following service file,

[Unit]
Description=Davmail Exchange Gateway

[Service]
Type=simple
ExecStart=/usr/bin/davmail %h/.config/davmail/davmail.properties
Restart=always
RestartSec=60

[Install]
WantedBy=default.target

Conclusion

Whichever way you did it, hopefully you were able to get everything connected up and working. Microsoft does not make it easy, but it is usually doable. I’ve been using DavMail to reliably access my Microsoft email from both Linux and BSD systems for over a year now, without any major issues.


  1. Ordinarily, setting up an email account in Evolution when you are using Gnome will make this email account available to certain other clients, like Geary. This does not appear to be the case with Exchange; even once configured, you will only be able to access your Exchange email and calendar in Evolution, not in Geary and Calendar. ↩︎