Sending and receiving mails under Linux usind msmtp and mutt over Microsoft Exchange EWS

Especially in Microsoft-embossed environments it might be needed to send and receiver mails over Exchange. Some graphical mail clients like Evolution are offering support for this - of course this is not a good solution for servers without graphical user interfaces.

DavMail is a Java-based platform-independent software that is able to act as Exchange gateway for the following protocols:

  • POP
  • IMAP
  • SMTP
  • CalDAV
  • CardDAV

The software listens on appropriate network ports and forwards requests over EWS (Exchange Web Services) to the Exchange server.

There are several versions of DavMail:

  • JEE web application (.war)
  • Bundle including graphical user-interface (for Debian-based distros)
  • Java standalone version
Note

Beginning with version 4.6.1 manually deploying the Java application and init scripts is obsolete! There are appropriate RPM packages in the download section of Sourceforge.

For servers you might choose the Java standadlone version because it can be used without a graphical user-interface. The archive is available for 32- and 64-bit systems. The first step is to download and extract the archive.

32-bit:

1# wget http://sourceforge.net/projects/davmail/files/davmail/4.5.1/davmail-linux-x86-4.5.1-2303.tgz/download -O davmail-linux-x86-4.5.1-2303.tgz
2# tar xfz davmail-linux-x86-4.5.1-2303.tgz
3# cd davmail-linux-x86_64-4.5.1-2303

64-bit:

1# wget http://sourceforge.net/projects/davmail/files/davmail/4.5.1/davmail-linux-x86_64-4.5.1-2303.tgz/download -O davmail-linux-x86_64-4.5.1-2303.tgz
2# tar xfz davmail-linux-x86-4.5.1-2303.tgz
3# cd davmail-linux-x86_64-4.5.1-2303

Hint: Before downloading the archive you might want to check if there is a newer version available: [click me!]

The software needs a configuration file including the OWA (Outlook Web Access) or EWS URL and other settings (SSL configuration, logging, etc.). This file is named davmail.properties - on the project website you can find an example: [click me!]

The most importan line defines the EWS/OWA URL:

1davmail.url=https://owa.domain.com

The other settings are documented detailed in comments.

DavMail is started using the following command:

1# ./davmail.sh davmail.properties

However it is nicer to start the software automatically while boot time. I published init scripts/service configurations For all common init/service control systems (SysV init, upstart, systemd) I published appropriate configurations on GitHub: [click me!]

This templates can be imported and used easily:

1sysvinit # wget https://raw.githubusercontent.com/stdevel/davmail-initscript/master/davmail-sysvinit -O /etc/init.d/davmail
2sysvinit # chmod +x /etc/init.d/davmail
3sysvinit # chkconfig --add davmail
4sysvinit # chkconfig davmail on
5sysvinit # service davmail start
1upstart # wget https://raw.githubusercontent.com/stdevel/davmail-initscript/master/davmail-upstart -O /etc/init/davmail
2upstart # initctl start davmail
1systemd # wget https://raw.githubusercontent.com/stdevel/davmail-initscript/master/davmail-systemd -O /usr/lib/systemd/system/davmail
2systemd # systemctl enable davmail.service
3systemd # systemctl start davmail.service

You might need to change paths in the configuration files if you installed DavMail in a different directory than /opt/davmail.

For sending mails msmtp is used in this case. This is a simple tool which relays mails over SMTP on a configured server.

A configuration file is created for msmtp - e.g. for the current user:

 1$ ~/.msmtprc
 2defaults
 3logfile ~/.msmtp.log
 4account default
 5host localhost
 6port 1025
 7protocol smtp
 8from max.mustermann@domain.com
 9auth login
10user domainmax.mustermann
11password MyPassword

The lines from, user and password need to be altered to match the Exchange configuration. The configuration can also be altered system-wide - to do this you need to write the content above in the file /etc/msmtprc. The protocol path (logfile) needs also be changed then.

Sending mails can be tested like this:

1$ echo -e "Subject: Test\r\n\r\nThis is a test mail" | msmtp bernd.beispiel@domain.com

If problems occur msmtp can be executed with the parameter -v to enable debugging.

For receiving mails using mutt the appropriate configuration (~/.muttrc) is altered:

 1set spoolfile="imap://max.mustermann:MyPassword@127.0.0.1:1143/INBOX"
 2set folder="imap://max.mustermann:MyPassword@127.0.0.1:1143"
 3set from="max.mustermann@domain.com"
 4set realname="max.mustermann"
 5set imap_user="max.mustermann@domain.com"
 6
 7set imap_pass="MyPassword"
 8set header_cache=~/.mutt/cache/headers
 9set message_cachedir=~/.mutt/cache/bodies
10
11set sendmail="/usr/bin/msmtp"
12my_hdr From: "max.mustermann"
13
14bind index G imap-fetch-mail

Beside IMAP/SMTP configuration a global hotkey G for pulling mails is defined. Don't forget to change user names and passwords.

Afterwards mutt is able to retreive mails over EWS.

Translations: