Sometimes you can’t be bothered to install and setup a command-line mail client and/or VPN, but you still need to access a POP 3 server from a remote machine. Sometimes you just need to know if a POP3 server is working or not. As a largely text-based protocol much like the HTTP protocol, telnet or openssl can be used to talk to a POP3 server and read some mail directly from the command line.
Establishing a connection
To start with, the usual process is to telnet to a POP3 server port, usually on TCP port 110. This would be very simple:
telnet mail.example.com 110
Nowadays, though, most POP3 servers are secured via SSL, usually sitting on port 995. If you try to use telnet on an SSL-only POP3 server, you’ll either get an error “Command is not valid in this state”, such as:
Trying 127.0.0.1...
Connected to mail.example.com.
+OK The Microsoft Exchange POP3 service is ready.
USER yiming
-ERR Command is not valid in this state.
or you’ll get a rather brusque brushoff
Trying 10.0.1.202...
Connected to mail2.example.com.
Escape character is '^]'.
USER yiming
Connection closed by foreign host.
When this is encountered, OpenSSL’s s_client should be used instead to perform the necessary SSL negotiations.
openssl s_client -connect mail.example.com:995
or
openssl s_client -crlf -connect mail.example.com:110 -starttls pop3
The second incantation is typically used for Microsoft Exchange servers. Note the -crlf option, which tells s_client to send \r\n line endings. If the wrong line ending is used for a server, the symptom is that the server will not respond to any commands. It will only sit there and wait for further input, while you are staring at a blank responses or blank lines in your telnet session.
Authentication
Having established a connection, it is now necessary to authenticate as a POP3 user. In the simplest case, plain text authentication is used. In this case, the command USER [username] is used to establish the username, and PASS [password] is used to establish the password in plaintext. (Since the connection is under SSL encryption, presumably this plaintext won’t matter).
+OK Server ready
USER yiming
+OK
PASS foobar
+OK Logged in.
Server interactions
Several commands are useful here.
- LIST – lists the messages available in the user’s account, returning a status message and list with each row containing a message number and the size of that message in bytes
- STAT – returns a status message, the number of messages in the mailbox, and the size of the mailbox in bytes
- RETR [message_num] – returns the message identified by the message number, which is the same as the message number shown in the LIST command output
- TOP [message_num] [n] – returns the top n lines of the message denoted by message number.
When finished, the QUIT command will end the session.
Conclusion
For other POP3 commands, such as commands marking deletion of a message, refer to RFC 1939, the canonical document defining the Post Office Protocol Version 3 ( POP3 ). At some point, if the commands to be tested become complicated, it may be more efficient use of time to install a mail client such as alpine.
See also my previous post on chatting with HTTP / HTTPS servers.
Thanks.
Thanks this post solve our Internal Team fighting between Network and Exchnage 😛
Exactly! I’m an Exchange admin and it’s getting old having to explain this to my users.
Thanks mate!
In Authentication process, I can’t login to my server.
SSH says
-ERR Invalid auth or access denied
How should I do?
Please help me.