Tutorial 07: Using TCP/IP to Check EmailApril 19, 2008© NS BASIC Corporation. All rights reserved.Contributed by Tom Newman, Mission Peak Designs |
The purpose of this tutorial is to demonstrate adding networking functionality to your NS Basic/Desktop programs. You should have completed Tutorial #1 before beginning this tutorial.
The program to be developed uses the NewObjects ActiveX Net Stream control to open a socket and connect with an email server. Then using the NewObjects Storage and File control to send and received data from the server.
Including network support in your application requires adding the NewObjects ActiveX control to the desktop. The control is automatically installed when you installed NS Basic/Desktop but you will need to install it on systems where NS Basic/Desktop is not installed.
(For a complete reference on the methods above, make sure you read the Tech Note 06: Communications - TCP/IP and IRDA and Tech Note 08: File I/O Control - SFStream)
Working with TCP/IP protocols is similar to reading/writing files from the file system. There are basically three steps used to talk to a server:
Because you are dealing with a remote system and not a local file, you have a "timeout" feature. It simply means that if your application does not hear from the remote machine for a specified period of time it will give up.
You must also specify to which service you want to connect in that remote machine, and that's the concept behind "port". Different ports provides different services, for instance,
Start a new "Standard" project and save it as HttpPop3.nsd
Create the following controls on the form:The form should look like the following:
In this project will simply connect to a pop3 server, send username and password, and display the result to the STAT command (it should tell how many messages you have).
Enter the following code in the Code Window (you can copy and paste this code):
Now let's analyze the code, line by line:
This is what is called a chat-style connection. You send something and wait for a response. That's how the POP3 server works:
You can tell if the response is a success or an error on POP3 protocol by looking at the first character of the response line: a plus means it was accepted, a minus means there was a problem.
Of course this is a very simple example to get you started. A real application would have functions to get lines and to send lines, and also check if the password was accepted, parse the STAT result lines to display only the number of messages and not the entire ugly line, and even retrieve the header of each message and display the sender, subject and date of all the emails, and much more.
Press F5 to save and start the program. Your results should look similar to the image below if you entered the proper email server address and a valid user name and password. The response from your email server may look a little different but you should see "+OK" as a response for every command sent. The message text box shows the program's progress and the responses. The "+OK 3675" in the last response is the number of emails on the server (I know, it's on my to-do list).