Tutorial 07: Using TCP/IP to Check Email

September 20, 2009

© NSB Corporation. All rights reserved.

Contributed by Tom Newman, Mission Peak Designs


Contents:

  1. Introduction
  2. How is it Done?
  3. Creating the Form
  4. Adding the Code
  5. Testing the Program

Introduction

The purpose of this tutorial is to demonstrate adding networking functionality to your NS Basic/CE 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.

How is it Done?

Including network support in your application starts as simply as adding NewObjects ActiveX control as a resource to your program. Select Project > Add Resource, then select newObjectsPack1.dll.

(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:

  1. First you have to open it (socket),
  2. Then you send commands and read data to/from it,
  3. and finally you close it (socket).

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,

Creating the Form

Start a new "Standard" project and save it as HttpPop3.nsb

Create the following controls on the form:

The form should look like the following:

Adding the Code

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.

Testing the Program

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).