Test fetching emails with IMAP and POP3

John will surely prefer to read his mail in a comfortable mail program. So he needs a way to get access to his mailbox. Two protocols come into play here:

  • POP3 (Post Office Protocol) is a simple protocol that lets you fetch email from a single mailbox. It is usually used to collect all emails, though you can also leave them on the server but this is a bit of a hack and you can't create multiple folders on the server to sort your mail. It saves space on the mail server because the email gets moved to the user's hard disk on their computer. But they won't be able to access the same email from another computer. Besides you cannot create multiple folders on the server to sort your mail. There is just the inbox. This variant is antiquated and not exactly user-friendly.
  • IMAP (Internet Messaging Application Protocol) is predominantly focused upon leaving your mail on the server but you can also collect it like POP3. The inbox is where your incoming emails are stored but users can also maintain folders and move emails to them. But users can move emails to different directories. IMAP is useful when you want to access your email from different locations without losing mail because you fetched it from another location. The drawback is that lazy users leave their mail on the server thus filling up your server's hard disk (unless you use quotas).

Testing POP3

Let us establish a POP3 connection and retrieve John's email:

$> telnet localhost pop3

The server replies:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.

Login as John:

user john@example.com

The server should accept that:

+OK

Send the password:

pass summersun

The server should recognize the correct password:

+OK Logged in.

Get a list of John's emails:

list

Dovecot will tell you that one email is in the mailbox:

+OK 1 messages:
1 474
.

Fetch that email number 1:

retr 1

Dovecot sends you the email:

+OK 474 octets
Return-Path: <steve@example.com>
X-Original-To: john@example.com
Delivered-To: john@example.com
Received: from example.com (localhost [127.0.0.1])
    by ... (Postfix) with ESMTP id 692DF379C7
    for <john@example.com>; Fri, 18 May 2007 22:59:31 +0200 (CEST)
Message-Id: <...>
Date: Fri, 18 May 2007 22:59:31 +0200 (CEST)
From: steve@example.com
To: undisclosed-recipients:;

Hi John,

just wanted to drop you a note.
.

Close the connection to the POP3 server:

quit

The server will disconnect you:

+OK Logging out.
Connection closed by foreign host.

Of course users won't use TELNET to read their mail. He will use a more comfortable email client. This is just to show you how POP basically works and to make sure Dovecot is behaving correctly.

Test IMAP

Instead of going through the following procedure (IMAP is rather complicated) you may as well just use mutt to create an IMAP connection:

$> mutt -f imap://john@example.com@localhost

Alternatively you can open up a raw IMAP connection to the server and enter the IMAP commands yourself:

$> telnet localhost imap2

You should get a connection:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK Dovecot ready.

IMAP commands always start with a number and reply to that command with the same number. So the following commands must be entered with the number at the beginning of each line. Login with username and password:

1 login john@example.com summersun

Dovecot logs you in:

1 OK Logged in.

Ask Dovecot for a list of John's mail folders:

2 list "" "*"

Here comes the list:

* LIST (\HasNoChildren) "." "INBOX"
2 OK List completed.

Select your inbox:

3 select "INBOX"

Dovecot gives you all kinds of information about that folder:

* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 1 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1180039205] UIDs valid
* OK [UIDNEXT 3] Predicted next UID
3 OK [READ-WRITE] Select completed.

You see that one email exists. Fetch it:

4 fetch 1 all

IMAP will just give you basic information on the email:

* 1 FETCH (FLAGS (\Seen) INTERNALDATE .........
4 OK Fetch completed.

To read the actual mail body you need to fetch it explicitly:

5 fetch 1 body[]

Here it comes:

* 1 FETCH (BODY[] {474}
Return-Path: <steve@example.com>
X-Original-To: john@example.com
Delivered-To: john@example.com
Received: from example.com (localhost [127.0.0.1])
        by ... (Postfix) with ESMTP id 692DF379C7
        for <john@example.com>; Fri, 18 May 2007 22:59:31 +0200 (CEST)
Message-Id: <...>
Date: Fri, 18 May 2007 22:59:31 +0200 (CEST)
From: steve@example.com
To: undisclosed-recipients:;

Hi John,

just wanted to drop you a note.
)
5 OK Fetch completed.

Disconnect from the server:

6 logout

Dovecot logs you out:

* BYE Logging out
6 OK Logout completed.
Connection closed by foreign host.

POP3 and IMAP appear to work. You could now use any email program like Kmail, Evolution or Thunderbird/Icedove and set up a POP3 or IMAP email account. The quickest way to check encrypted connections is using mutt again:

$> mutt -f imaps://john@example.com@localhost

If you use other mail programs note that the username will be the email address 'john@example.com' and the password is 'summersun'. You can try these kinds of connections:

  • POP3
  • IMAP
  • POP3 with TLS/SSL enabled
  • IMAP with TLS/SSL enabled

When using TLS/SSL you will probably get a warning that the certificate of the server cannot be trusted. Dovecot creates a self-signed certificate. If you are not happy with the certificate then create your own:

$> openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/certs/dovecot.pem \
-keyout /etc/ssl/private/dovecot.pem

The certificate and key will be created while you get asked a few questions:

Generating a 1024 bit RSA private key
.........++++++
............................++++++
writing new private key to '/etc/ssl/certs/dovecot.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:Hamburg
Locality Name (eg, city) []:Hamburg
Organization Name (eg, company) [Internet Widgits Pty Ltd]:workaround email service
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:mailtest.workaround.org
Email Address []:postmaster@workaround.org

Of course you should fill in your own information here. The most important setting is the Common Name which must contain the fully-qualified name of your mail server. Oh, and this certificate will be valid for 10 years (3650 days) - adjust that period as you want.

Do not forget to set the permissions on the private key so that no unauthorized people can read it:

$> chmod o= /etc/ssl/private/dovecot.pem

And you will have to restart Dovecot to make it read your new certificate:

$> /etc/init.d/dovecot restart

 

Comments

dovecot Can't load private key file /etc/ssl/private/dovecot.key

Hi and thank you for this great tutorial - i'm reading and using it since a bunch of years!

After setting up a new Server and trying to install ssl-key's my dovecot was running into trouble (syslog):
dovecot: child 3186 (login) returned error 89
dovecot: child 3187 (login) returned error 89
dovecot: child 3188 (login) returned error 89
dovecot: child 3189 (login) returned error 89
dovecot: pop3-login: Can't load private key file /etc/ssl/private/dovecot.key: error:02001002:system library:fopen:No such file or directory
dovecot: imap-login: Can't load private key file /etc/ssl/private/dovecot.key: error:02001002:system library:fopen:No such file or directory
dovecot: pop3-login: Can't load private key file /etc/ssl/private/dovecot.key: error:02001002:system library:fopen:No such file or directory
dovecot: imap-login: Can't load private key file /etc/ssl/private/dovecot.key: error:02001002:system library:fopen:No such file or directory
dovecot: child 3190 (login) returned error 89
dovecot: imap-login: Can't load private key file /etc/ssl/private/dovecot.key: error:02001002:system

I'm not sure i misconfigured something or my installation was not 100percent debian compliant (in this case please delete this post!), but i solved the problem with a little change in key generation string:
openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/certs/dovecot.pem -keyout /etc/ssl/private/dovecot.key

For keyout i had to use "dovecot.key" instead of 2nd time "dovecot.pem" - maybe that's because i tried not only one time to generate the certificate, maybe it was only a mistake in writing. I never looked so deep into certificates because the mailserver is the only point i'm using it atm ;-)

Thank you
Jan

Whether you call the private

Whether you call the private key dovecot.pem, dovecot.key or pizza-tonno.exe doesn't matter. By default Debian's Dovecot installation reads:

#ssl_cert_file = /etc/ssl/certs/dovecot.pem
#ssl_key_file = /etc/ssl/private/dovecot.pem

So putting the private key into /etc/ssl/private/dovecot.pem and the public key into /etc/ssl/certs/dovecot.pem is the standard way. You are free to rename the files or put them elsewhere as long as you tell Dovecot.

cant login via pop3 or imap

logging in per mutt works perfectly, but i can't log in per pop3 or imap. any idea what i did wrong?

console says:

telnet localhost pop3
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
john@example.com
-ERR Unknown command.
user john@example.com
+OK
pass summersun
+OK Logged in.
-ERR Internal error occurred. Refer to server log for more information. [2010-02-25 16:29:32]
Connection closed by foreign host.
 

log:

Feb 25 16:20:01 servername postfix/qmgr[7562]: BD1F5BCE87E3: removed
Feb 25 16:29:32 servername dovecot: auth-worker(default): mysql: Connected to 127.0.0.1 (mailserver)
Feb 25 16:29:32 servername dovecot: pop3-login: Login: user=<john@example.com>, method=PLAIN, rip=127.0.0.1, lip=127.0.0.1, secured
Feb 25 16:29:32 servername dovecot: POP3(john@example.com): open(/var/vmail/example.com/john/Maildir/cur/1267101080.25858_2.lvps83-169-34-252:2,RS) failed: Permission denied
Feb 25 16:29:32 servername dovecot: POP3(john@example.com): Couldn't init INBOX: Internal error occurred. Refer to server log for more information. [2010-02-25 16:29:32]
Feb 25 16:29:32 servername dovecot: POP3(john@example.com): Mailbox init failed top=0/0, retr=0/0, del=0/0, size=0

Thanks for help,

Jens

Permissions?

open(/var/vmail/example.com/john/Maildir/cur/1267101080.25858_2.lvps83-169-34-252:2,RS) failed: Permission denied

This looks suspiciously like you missed to set proper directory permissions somewhere in /var/vmail.

still

First of all thanks for the great tutorial, and the fast reply!
i'm sorry that i have to say that i still was not able to solve the problem.
i even deleted the vmail user and set it up with a different id, but sitll the same error.
do you have an other hint for me?

 

thanks a 1000 times,

 

jens

mutt works, but not evolution or kmail

When I use mutt or telnet, I can read the emails correctly,

but using a common email program, I always get an error:

-ERR, Authentication failed, or similar.