Setting up Mutt and IMAP for email through Exchange EWS

One of the nice things about working for Canonical these past many years was having an entirely Linux-based infrastructure. For the most part I never needed to worry about the existance of Windows.

Yet, having now joined a company, that – like most of the modern world – is fundamentally Microsoft infrastructured, I find myself needing to interface with Exchange to get and send my email.

At first I took the easy route of using Outlook webmail in firefox. This wasn’t total frustration, I actually kinda liked the conversation grouping feature. But inevitably the frustrations eventually hit a breaking point!

I assumed setting up mutt with Exchange would be a well trod path on the Internet and I could just paint-by-numbers my way through. No such luck. So, in hopes of saving others some work of piecing things together from man pages, random forum posts, and plain trial and error, here’s my guide for getting this all set up.

Certain Linux-based mail clients like Thunderbird and Evolution are able to speak directly with Exchange using EWS. However, there are many other Linux email and calendaring clients available that you may want to use, but that don’t support Exchange EWS. For instance, you may wish to use a console-based mail client such as mutt.

Fortunately, you can set up DavMail as an intermediary gateway. DavMail interfaces with the Exchange server, presenting it as a standards-compliant POP/IMAP/SMTP/Caldav/Carddav/LDAP service. In other words, you use IMAP or POP to browse or retrieve emails, SMTP to send email, LDAP to look up addresses, Caldav for calendaring, and Carddav for personal contacts.

Setting up the DavMail server

The DavMail package can be downloaded from http://davmail.sourceforge.net/download.html. At the time of this writing it appears to not exist in the Ubuntu archive, however a .deb file suitable for Debian and Ubuntu is available from the above link. For other Linux distributions you’ll need to use the standard tarball package and manually install Java.

  # Install on Ubuntu
  sudo apt-get install libswt-gtk-3-java   # Or other java as desired
  sudo dpkg -i davmail_4.3.3-2146-1_all.deb

You can then launch DavMail via the Applications/Internet menu.

DavMail does not store any credentials, it merely needs to know the name and path of the Exchange server to use. The first time you launch DavMail, a configuration dialog will appear. Only one parameter needs changed:

  URL OWA (Exchange):  https://webmail.your.company.com/EWS/Exchange.asmx

The rest of the settings can be left at defaults. See the DavMail Tutorial for info about the rest of the settings or if you run into problems. Note that logging information is written to ~/davmail.log.

Finally, set up DavMail to start up on session login. For example:

  echo "davmail &" >> ~/.gnomerc

I’d never heard of DavMail before, but it’s been quite solid so far.

Setting up msmtp SMTP client

Sendmail is the de facto standard for SMTP, but is a bit overkill if you’re just setting up a single user local email environment. msmtp is simpler to configure and adequate for single user email needs.

  sudo apt-get install msmtp
  touch ~/.msmtprc ~/.msmtp.log
  chmod 600 ~/.msmtprc ~/.msmtp.log

Since the IMAP server is on localhost, the msmtp configuration is quite basic:

  # ~/.msmtprc
  defaults
  logfile ~/.msmtp.log

  account default
  host localhost
  port 1025
  protocol smtp
  from f.lastname@company.com
  auth AUTH
  user DOMAIN\f.lastname
  password  PASSWORD

Here, PASSWORD is your own password, DOMAIN is going to be something specific to your company, and for AUTH it’ll depend on how you want to have things set up. See msmtp authentication for options.

Setting up mutt for email

If you’ve used mutt before, you’ll already be familiar with its myriad configuration options. If you haven’t, then this section isn’t for you!

So, skipping over installation and basic mutt configuration, here’s how to configure mutt to use IMAP. This keeps the emails and folders on the remote server and uses the local DavMail service for sending and receiving emails.

  # ~/.muttrc
  set spoolfile="imap://f.lastname:PASSWORD@127.0.0.1:1143/Inbox"
  set folder="imap://f.lastname:PASSWORD@127.0.0.1:1143"
  set from="f.lastname@company.com"
  set realname='Firstname Lastname'
  set trash="imap://f.lastname:PASSWORD@127.0.0.1:1143/Trash"
  set imap_user="f.lastname@company.com"
  set imap_pass="PASSWORD"
  set imap_keepalive=900   # Set lower if connection times out
  set header_cache=~/.mutt/cache/headers
  set message_cachedir=~/.mutt/cache/bodies

  set sendmail="/usr/bin/msmtp"
  my_hdr From: "Firstname Lastname"

Setting up git

You can direct git to send patches through SMTP via the DavMail server.

  # ~/.gitconfig
  [user]
        name = Firstname Lastname
        email = f.lastname@company.com

  [sendemail]
        smtpserver = localhost
        supresscc = self
        from = Firstname Lastname 
        smtppass = PASSWORD
        smtpserverport = 1025
        smtpuser = f.lastname
        confirm = auto
	chainreplyto = true

8 Responses to Setting up Mutt and IMAP for email through Exchange EWS

  1. Guillaume Laferriere says:

    Thanks for this excellent tutorial.

    I was able to get Mutt to read my owa mails from work. I struggled a bit to get pass the fetching phase when starting Mutt. It kept on refetching from message 1 without writing the header cache.

    I had to do 2 small changes to ~.davmail.properties

    # Don't fetch all the emails... only the newest 500. # I was reading from a 12000 mails inbox and could't get past the fetching pass. davmail.folderSizeLimit=500

    # Ran into a problem when sending mails # ErrorSendAsDenied ERROR davmail - ErrorSendAsDenied The user account which was used to submit this request does not have the right to send mail on behalf of the specified sending account. # http://sourceforge.net/p/davmail/discussion/644057/thread/514567ff/ davmail.smtpStripFrom=true

  2. saju says:

    How to Setting up Mutt Email Client with IMAP and SMTP for Receive and Send Email through Exchange Web Services EWS using DavMail

    https://www.youtube.com/watch?v=ILFc8c1ekCU

  3. Heya, Bryce! Heh heh, kinda fun to find your friends’ sites in the top results when you search a web index for something as nerdy as “exchange ews for mutt”. I’ve tried Thunderbird off-and-on, and even, OWA (web client) and Apple Mail, but nothing is ever as quick and simple as mutt. Admittedly I am disappointed that you didn’t write an EWS or ActiveSync interface for mutt! :P Joking of course. I’ve done the DavMail thing too. Very handy, although I still long for something more pure. Apparently IMAP became a maintenance issue where I work for some reason. They just sorta disabled it one day!

    Great article writeup, as usual! I’m sure I’ll see ya around!

  4. I am curious if you can shed any light into why your sysadmins did not enable IMAP in Exchange Server? We had it enabled at one point sometime last year. When I asked why it was shut off, the answer was “why do you need IMAP”? Since then I’ve pretty much learned to stop worrying and embrace the Microsoft culture, no matter how much it hurts as they force it down my throat! EWS API is pretty simple, but I’m not sure how many people might be interested in an EWS interface for mutt. Judging by the fact that I stumbled on a personal friend’s blog during the initial research, I’d say we must be a pretty small crowd. Long live DavMail.

  5. David says:

    Thanks Bryce for a good tutorial!

    I wonder if you know how to use a setup that sync mails and saves them locally. I first tried davmail and now I’m trying offlinimap which I think is faster and alse neat to have all mails available offline. The downside is that it’s hard to find a clean solution also for my work calendar and the global addressbok which my company use. If it’s possible to use davmail with all these features it would be perfect (at least for me)

  6. bryce says:

    Hi David,

    I used davmail with POP (instead of IMAP) to save mails locally.

  7. bryce says:

    Tony, yeah good question why they didn’t just enable IMAP. I think it was a combination of overzealous security policies and lack of awareness of non-Windows platforms.

    Luckily one of our managers worked out getting us our own server, so we’re relaying mail through it, no need for davmail at all any more. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>