December 18, 2008

The Telnet Functions – Get POP3 Message Count

Filed under: Automation,Scripting — Marcus Tettmar @ 1:41 pm

Recently in a forum post, “Getting New Email Message counts” terencepjf asked:

Does anyone know a way to get a count of New Email messages:

1) Without downloading all the messages
2) Count of messages in Inbox Sub-Folders

RetrievePOP3 works but the files need to be downloaded.”

My answer is to use the Telnet functions. You might not realise that POP3 – the protocol used to download email – is a Telnet protocol. You can Telnet into your POP3 server and see the messages waiting. POP3 usually runs on port 110, so you would telnet into the mail server on port 110. Try this from a command prompt (you type the italics):

telnet your.mail.server 110
+OK Hello there.
user your_username
+OK Password required
pass your_password
+OK Logged in
STAT
+OK 3 325657

So you simply issue “user” followed by your username, then “pass” followed by your password to log in. There are then various commands you can issue. STAT returns the number of messages waiting on the server and their size. Note that STAT returns a string in the format:

+OK MESSAGE_COUNT MESSAGE_SIZE

There are other commands you could use: LIST gets a list of the messages on the server, with their sizes. RETR n would retrieve one of them.

So we could use the STAT command to get the number of messages on the server. This is a useful exercise in the Macro Scheduler Telnet functions:

TelnetConnect – connect to a telnet server
TelnetSend – send a string to a telnet server
TelnetWaitFor – wait for a specific response and returns the text received
TelnetClose – close the session

So here’s the code to connect to a pop3 server, enter username and password and issue the STAT command, then disconnect:

TelnetConnect>mail.server.com,110,pop3
TelnetSend>pop3,user YOUR_USER_NAME%CRLF%
TelnetWaitFor>pop3,Password required,5,resp
TelnetSend>pop3,pass YOUR_PASSWORD%CRLF%
TelnetWaitFor>pop3,logged in,5,resp
TelnetSend>pop3,STAT%CRLF%
TelnetWaitFor>pop3,OK,5,resp
TelnetClose>pop3

Line 1 connects to the mail server on port 110 and returns a session variable which we will use to access this session with the other commands.

Line 2 sends “user YOUR_USER_NAME”. Note that we also send a CRLF, otherwise it would be as if we didn’t press return. Nothing happens until the telnet server receives a CRLF.

Line 3 waits for “Password required” to be returned. We then know the server is ready to receive the password.

Line 4 sends “pass YOUR_PASSWORD” followed by the obligatory CRLF.

Line 5 waits for “logged in” to be returned.

Line 6 then sends the STAT command and Line 7 waits for it to complete (waits for “OK”).

The TelnetWaitFor command returns the response in a variable, which we have called “resp” above. We can now parse this to determine the number of messages waiting. As noted above STAT returns the message count in the second column. So we just need to do:

Separate>resp,SPACE,parts
Let>count=parts_2
MessageModal>There are %count% messages waiting

In other words, we split the returned lines into an array delimited by the SPACE character. So we end up with three entries, where the second is the one we want.

So the full script to connect to a mail server and retrieve the message count is:

TelnetConnect>mail.server.com,110,pop3
TelnetSend>pop3,user YOUR_USERNAME%CRLF%
TelnetWaitFor>pop3,Password required,5,resp
TelnetSend>pop3,pass YOUR_PASSWORD%CRLF%
TelnetWaitFor>pop3,logged in,5,resp
TelnetSend>pop3,STAT%CRLF%
TelnetWaitFor>pop3,OK,5,resp
TelnetClose>pop3
Separate>resp,SPACE,parts
Let>count=parts_2
MessageModal>There are %count% messages waiting

Only 11 lines of code. It will work quickly and transparently without having to download anything to disc. More to the point, it’s a useful example on how to use the Telnet functions. Hope you find it helpful.