Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Reading emails from VFP without additional components - Part I
Roberto C. Ianni, January 1, 2005
This article is the beginning of receiving email from VFP; this is complemented with the articles you have already seen about sending email from VFP. To be able to do this, we will have to use the POP3 protocol (Post Office Protocol 3).
Summary
This article is the beginning of receiving email from VFP; this is complemented with the articles you have already seen about sending email from VFP. To be able to do this, we will have to use the POP3 protocol (Post Office Protocol 3).
Description

This article is the beginning of receiving email from VFP; this is complemented with the articles you have already seen about sending email from VFP. To be able to do this, we will have to use the POP3 protocol (Post Office Protocol 3).

As many of you already know, it is my habit to see how things really work. For this purpose, we will have to investigate about the POP3 protocol, to see how it works, what its commands are, how the server works, etc. Thus, we continue with this series of articles; in this case, it is about receiving emails without third-party products, using only VFP.

I want to note that if you understood how SMTP works, understanding POP3 will be quite easy.

Objectives

Like SMTP, POP3 is a protocol that exchanges text between the client and the server applications. For this purpose, POP3 uses a series of commands supported by the server, so that the client can ask for the list of messages that the user has, request a specific message, and even erase it. By default, POP3 is available on port 110.

The points that we will develop are:

  1. Standard commands.
  2. Optional commands.
  3. Control the commands sent to POP3.
  4. Connection via telnet.
  5. POP3 authentication.
    1. Plain-text authentication.
    2. MD5 authentication.
  6. Parsing the email.
  7. Receiving attachments.
    1. Text files and binary files.
    2. Decode base 64.
  8. Implementation of the class WPop3Mail.
  9. Test application.

Standard commands

As a first step, we will learn the standard commands that should be available in all POP3 servers, that allow us to download our messages from the server, which were previously sent there via the SMTP protocol.

Command Description
USERSend the username to the server, always plain-text.
PASSSend the user password, always plain-text.
STATReturn the message count on the server, and the total size in bytes of the messages.
LISTReturn the size in bytes of a specific email on the server.
RETRThis command requests the POP3 server to send a complete message to the client.
DELEThis command requests the POP3 server to erase a specific message from the server.

With these basic commands, we can now interact with the POP3 server, to ask it whether there are new message, to download the messages, and even to delete them if we don't want them on the server.

Optional commands

The optional, or extended, commands allow us a greater control, and greater security, with the messages on the server.

Command Description
APOPSend the user keyword, encrypted with the MD5 algorithm.
TOPFetch only the header of the specified message.
UIDLReturns the UIDL (Unique ID Listing RFC1939). This is a unique number assigned to each message; it is not based on the position of the message in the list.

Control the commands sent to the POP3 server

The method to control the messages that return from the POP3 server is more simplistic than with SMTP messages, since here we have only two variants, either it is good or it is bad; unlike SMTP, which has different error codes depending on the command sent.

Return value Description
+OKThe command sent was resolved correctly.
-ERRThe command doesn't exist, its use isn't pertinent, or the operation can't be done.
USER ianni_roberto@server.com.ar
+OK password required for user ianni_roberto@server.com.ar
PASS 123456789
-ERR [AUTH] Authentication failed

As we can see, the result of the commands comes with the "+OK" or "-ERR", and a descriptive message.

Telnet conection

Just like SMTP, you can also consult POP3 with a simple telnet, and thus access the messages stored on the server. The problem, as we shall soon see, is to understand the email with telnet!

If we run the following command from a console, we will have the conection, and we can pass the commands which we have just seen.

C:\Telnet server.com.ar 110

<abcde123654fghi>
USER ianni_roberto@server.com.ar
+OK password required for user ianni_roberto@server.com.ar
PASS 123456789
+OK
STAT
+OK 5 13150
LIST 1
+OK 2630
RETR 1
+OK
Return-Path: <ianni_roberto@hotmail.com>
Delivered-To: CI-ianni_roberto@server.com.ar
Received: (qmail 2516 invoked from network); 31 Aug 2004 10:55:57 -0000
Received: from unknown (HELO postino9.prima.com.ar) (172.16.1.100)
  by cumeil13.int.prima.com.ar with SMTP; 31 Aug 2004 10:55:57 -0000
Received: (qmail 63214 invoked by uid 10006); 31 Aug 2004 10:55:57 -0000
Received: (Antivirus 0e1d36106fc2798aa1704741707bfb9c); 31 Aug 2004 10:55:57 -0000
Received: (qmail 63186 invoked from network); 31 Aug 2004 10:55:56 -0000
Received: from unknown (HELO hotmail.com) (64.4.47.34)
  by postino9.prima.com.ar with SMTP; 31 Aug 2004 10:55:56 -0000
Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
    Tue, 31 Aug 2004 03:55:54 -0700
Received: from 201.254.49.143 by by9fd.bay9.hotmail.msn.com with HTTP;
   Tue, 31 Aug 2004 10:55:54 GMT
X-Originating-IP: [201.254.49.143]
X-Originating-Email: [ianni_roberto@hotmail.com]
X-Sender: ianni_roberto@hotmail.com
From: "Roberto Ianni" <ianni_roberto@hotmail.com>
To: ianni_roberto@server.com.ar
Bcc:
Subject: Test POP3
Date: Tue, 31 Aug 2004 07:55:54 -0300
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_4c56_2fea_2f76"
Message-ID: <BAY9-F34pD0mApR0Hy900018e35@hotmail.com>
X-OriginalArrivalTime: 31 Aug 2004 10:55:54.0449 (UTC) FILETIME=[16679C10:01C48F49]

This is a multi-part message in MIME format.

------=_NextPart_000_4c56_2fea_2f76
Content-Type: text/plain; charset=iso-8859-1; format=flowed

texto

_________________________________________________________________
MSN Amor: busca tu ½ naranja http://latam.msn.com/amor/

------=_NextPart_000_4c56_2fea_2f76
Content-Type: application/x-zip-compressed; name="Adjunto1.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Adjunto1.zip"

UEsDBBQAAAAIAPM7cS5OisqexwAAADYEAAAHAAAAQXBwLmljb+2SsQrCMBCG
/7SBbtJHyCQ+hoNQhaKv0kFwvcHBx8rQUfocPoCDW7xLKqaRUrC4+SWXy11L
cuR+QCGDMSWEWwYs2ZdliFcKWOTseW/Y1mwK2n+jDCOQnyAKToakeLHWcsb5
AAAIAA1rci7kSbemvwIAAGkKAAALAAAAAAAAAAEAIAC2gR0XAABNYWlsSXRl
bS5jc1BLBQYAAAAABwAHAJgBAAAFGgAAAAA=


------=_NextPart_000_4c56_2fea_2f76
Content-Type: application/x-zip-compressed; name="Adjunto2.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Adjunto2.zip"

UEsDBBQAAAAIAGKotS6k8Xq7bQUAABsVAAAbAAAAWWFob29NYWlsQ2hlY2tl
ci9mcm1ZTUMuZnJt3Vfrb9s2EP8eIP8Dp36YjcWqHpbjdEsBP1sDzgO1myyf
AAAAYqi1LgAAAAAAAAAAAAAAABEAAAAAAAAAAAAQAP9B2yMAAFlhaG9vTWFp
bENoZWNrZXIvUEsFBgAAAAAGAAYAygEAAAokAAAAAA==


------=_NextPart_000_4c56_2fea_2f76--
DELE 1
+OK
QUIT
+OK

As we can see, first we identify ourselves with the server, so that the server knows who is the user connected. Then, we check for the number of emails in our inbox; together with the number of messages, the server returns the number of bytes they use. Once we have done that, we request the first message with the command "RETR"; the server replies with the message "+OK" and then sends us the complete message which is saved on the server.

When we get the message back, we can see that it is no more and no less than the message we have been sending with SMTP, therefore, we can understand it clearly. Within the message, we find the originator, the recipient, the subject, etc. Also, we see that this email contains two attachments, separated by the "boundary" we learned about in SMTP.

Once we received the message, we delete it from the server with the "DELE" command, and then we disconnect.

Conclusion

At the first sight, it looks difficult to read a message with these characteristics. What we have to do is locate, one part at a time, what we want to see. As you can imagine, the program that receives this has to do the same "parsing" of the text, to understand one part at a time, and show it to us in a simple format; but we will see this later.

Roberto C. Ianni, Banco Hipotecario
Roberto Ianni (Buenos Aires, Argentina) is a professional software developer for the last 4 years. He has programmed in C++, VFP, VC++, C#, and has lots of experience in VFP and VC++. He currently works in the Banco Hipotecario as an Analyst-Developer.
More articles from this author
Roberto C. Ianni, December 1, 2004
Today we are going to talk about data compression. This is something we do frequently, something which is useful for many different tasks.
Roberto C. Ianni, June 1, 2004
The "EventLog" or "Event viewer" is something we use on a daily basis, to obtain information about what happened to our computer at a certain moment; for example, when an application generates an error, most of us will intuitively look at the EventLog to see what information it left for us.
Roberto C. Ianni, May 1, 2004
The "EventLog" or "Event viewer" is something we use every day, to obtain information about what happened to our computer at a certain moment. For example, when an application generates an error, most of us instinctively go to the EventLog to see what information it left for us.
Roberto C. Ianni, July 1, 2004
One of the great limitations I find in VFP is the lack of low-level manipulation I can obtain from it. For many, this might not be an inconvenience, but several times I have encountered a problem which can't be solved with Fox, and it is then that I use a DLL or an FLL.
Roberto C. Ianni, August 1, 2004
As an objective for this issue, I want to propose the following points, to complete the first part of the development of an FLL. Advanced development with an FLL; Accessing VFP data; Executing VFP commands from a FLL; MultiThreading; FLL with VFP 9.0; Executing Assembler from VFP.
Roberto C. Ianni, October 1, 2004
This article is the continuation of "Sending email through Visual FoxPro without additional components", published last March. Back when I wrote this article, I didn't think it would get such a huge response, but it did, hence, due to the mails coming from everywhere I decided to write again on that...
Roberto C. Ianni, November 1, 2004
In this second part we will learn how to: Implement MIME, Attach binary files, the final implementation of the WSendMail class, and the use of the WSendMail class.
Roberto C. Ianni, May 1, 2005
Windows Control Panel holds the configuration from several of the operating system's applications, and from some other applications that provide an applet to appear there. Learn how to build this kind of applets to give your application a professional configuration mechanism.
Roberto C. Ianni, April 1, 2005
The intention of this article is to finish with the use of POP3 (Post Office Protocol 3), implementing everything we have seen so far.
Roberto C. Ianni, February 1, 2005
We will continue some of the points mentioned in the previous article, in order to be able to implement message reception. The points to be developed are: POP3 Authentication (both Flat and MD5 authentication methods) and EMail parsing.
Roberto C. Ianni, March 1, 2004
Many of us send email using third-party tools such as Outlook Express or Microsoft Outlook, among others, and most likely have encountered registration problems in the client, licencing problems, OLE errors that only occurr at the client's site but not in our developer environment. Also problems if ...
Roberto C. Ianni, August 1, 2006
The present article is one of those that Martín Salías calls "low level fox", the general idea is to develop a service for the operative system whose only goal will be to run a VFP application in charge of handling the business logic.