Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 2: Creating a Simple Email Client using Java and POP protocol, Lab Reports of Computer Science

A lab exercise for creating a minimal email reader using java programming and the pop protocol. Students will learn how to create a gui interface, connect to the email server, log in, retrieve and display email messages, and handle server responses. The document also covers good programming practices such as commenting, formatting, and using if statements.

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-b47
koofers-user-b47 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 2
A Minimal Email Reader
Due: 17:00 Saturday 13 September
Your goal in this lab is to use the knowledge of the POP protocol you gained in last week’s lab together with the
introduction to Java programming provided in class to write a very simple email client program. The program you
write will actually only provide half of the minimal functionality of an email client. It will enable its user to read
mail but not provide the ability to send mail.
This handout provides guidance on the steps you will need to complete as you construct this program. You should
read the entire handout carefully before coming to lab. In addition, you should sketch out the code you think
you will need for at least the first steps of the lab on paper before your lab period.
In this handout, we first describe the interface that your program should provide. We then discuss the major steps
you will have to take to complete the program. Next, we review the details of the process of entering your pro-
gram’s code, running your program, and electronically submitting your work when you are done. After that, we
explain what we will look for when we grade it. Finally, we provide information on a number of details you will
need to complete this program including a short review of the POP protocol.
A POP Client Interface
A sample of what the interface for your program might look like while the program is being used is shown below.
Across the top of its window, the program displays two text fields in which the user must enter his or her account
information. Below these is a field to hold the number of the message the user wants to see. This field is followed
by a button labeled “Get Message.’’The remainder of the program’s window is filled with two JTextAreas. The
upper text area is used to display the latest message retrieved. The text area that appears near the bottom of the win-
dow is used to display the “+OK” and “-ERR” messages the POP server sends in response to the commands it re-
ceives from your program.
While experimenting with the Apple Mail program, we learned that it fetches all available messages at once. Your
program will take a different approach. Each time the “Get Message’’ button is pressed, it will log on to the CS de-
partment’s email server using the “USER’’ and “PASS’’ commands, retrieve the specified message using the
“RETR’’ command, and then log out using the “QUIT’’ command. As a consequence, the user of the program must
fill in all three text fields before pressing the “Get Message’’ button.
CMSC 150: Intro to Computing Fall 2008
1
pf3
pf4
pf5

Partial preview of the text

Download Lab 2: Creating a Simple Email Client using Java and POP protocol and more Lab Reports Computer Science in PDF only on Docsity!

Lab 2

A Minimal Email Reader

Due: 17:00 Saturday 13 September

Your goal in this lab is to use the knowledge of the POP protocol you gained in last week’s lab together with the introduction to Java programming provided in class to write a very simple email client program. The program you write will actually only provide half of the minimal functionality of an email client. It will enable its user to read mail but not provide the ability to send mail. This handout provides guidance on the steps you will need to complete as you construct this program. You should

read the entire handout carefully before coming to lab. In addition, you should sketch out the code you think

you will need for at least the first steps of the lab on paper before your lab period. In this handout, we first describe the interface that your program should provide. We then discuss the major steps you will have to take to complete the program. Next, we review the details of the process of entering your pro- gram’s code, running your program, and electronically submitting your work when you are done. After that, we explain what we will look for when we grade it. Finally, we provide information on a number of details you will need to complete this program including a short review of the POP protocol.

A POP Client Interface

A sample of what the interface for your program might look like while the program is being used is shown below. Across the top of its window, the program displays two text fields in which the user must enter his or her account information. Below these is a field to hold the number of the message the user wants to see. This field is followed by a button labeled “Get Message.’’The remainder of the program’s window is filled with two JTextAreas. The upper text area is used to display the latest message retrieved. The text area that appears near the bottom of the win- dow is used to display the “+OK” and “-ERR” messages the POP server sends in response to the commands it re- ceives from your program. While experimenting with the Apple Mail program, we learned that it fetches all available messages at once. Your program will take a different approach. Each time the “Get Message’’ button is pressed, it will log on to the CS de- partment’s email server using the “USER’’ and “PASS’’ commands, retrieve the specified message using the “RETR’’ command, and then log out using the “QUIT’’ command. As a consequence, the user of the program must fill in all three text fields before pressing the “Get Message’’ button.

Implementation plan

It is a good idea to implement a program in small steps, testing the correctness of what you have done in each step as completely as possible before moving on to the next step. Otherwise, it can be very difficult to pinpoint errors when they occur. With this in mind, here is a plan you may wish to follow while working on this assignment:

  1. Write the declarations and instructions to construct the elements of the desired user interface. This code should resemble several examples you have seen in class or in the text. To ensure that your password is not visible while you are running your program you should use a JPasswordField rather than a JTextField in your program’s interface. A brief discussion of how to use a JPasswordField is provided later in this handout. Test that the code you write works by running the program (as described later in this handout) to verify that the interface appears as desired. In fact, you will probably want to run the program as soon as you have written enough instructions to create the first few elements of the interface and repeatedly as you add more components.
  2. Add statements to create a NetConnection. Recall that a POP server sends a welcome message as soon as a client establishes a connection with it. As a result, in addition to creating the connection, you should write code to retrieve and display this welcome message in your program’s upper text area. a. Declare an instance variable to refer to your NetConnection, but place the assignment statement that ac- tually constructs the connection and associates it with the variable name in the buttonClicked method, which you will also need to define. b. After the code that creates the NetConnection, place an instruction to add the program as a message lis- tener for the connection. Doing this will indicate to the program that you plan to handle all messages re- ceived from the server within a separate dataAvailable method. That is, your final code is likely to look more like the code in Figure 4.11 of “Programming with Java, Swing, and Squint” than like Figure 4.6. c. Define a dataAvailable method. Recall that the instructions in this method will be executed each time a message arrives from the server (including the welcome message). Place code in this method to retrieve a line from the NetConnection and display it in the program’s larger text area. Don’t forget to add a new line character (\n) to the end of each line. Once you have completed these steps, run your program. If everything is correct, a message that begins with “+OK” should appear in your program’s window. Note that when you run your program, the “+OK” from the server appears in the text area at the top of your window. This is not quite what we want. The content’s of the email message retrieved should be displayed in the upper text area, while the “+OK” messages appear separately in the lower text area. For now, however, leave your code as it is. We will address the placement of messages in the correct areas in the step 6.
  3. Next, actually send a command to the server. Start with the simplest command, “QUIT”. This should cause the server to send back another “+OK” line. Run your program and make sure that this extra line appears.
  4. Programs that use NetConnections should close the connections once they are no longer needed. Your pro- gram does not currently do this. Luckily, when you send a “QUIT” command the server will close its end of the connection. Therefore, you can easily arrange to close your end of the NetConnection by defining a method named connectionClosed. The code in this method will be executed as soon as the server closes the connec- tion. Place code to close your end of the NetConnection in this method’s body. To verify that the connection is actually being closed, include code in your connectionClosed method that will append a message to your text area indicating that the connection has been closed.
  5. Now that the connection is being constructed and closed correctly, you can write instructions to send the com- mands required to login and to fetch a message. Place the instructions to send these POP commands in your buttonClicked method before the code that sends the “QUIT” command. For each instruction sending a POP command, follow that instruction immediately by an instruction to append to the lower JTextArea the infor- mation just sent.
  6. As explained above, the upper text area in your program’s window is supposed to hold the text of email mes- sages while the lower text area should hold the “+OK” responses from the server. We postponed implementing this behavior because it requires a feature of Java that we have not yet discussed in class, the if statement. Later in this handout we provide enough information about if statements to enable you to write the required code. Our hope is that this limited, but practical experience using an if statement in lab will enable you to bet-
  1. Select the first item in the menu. It should look like “new EmailReader()”. This tells BlueJ to create an instance of your class and to execute the instructions you placed in its constructor. Just use the default name for your instance provided by BlueJ. An icon for this new instance will appear in the bottom of the project window. A window will appear on the screen and any GUI components your code creates will appear within the window. You should now be able to interact with your program. Try entering a user id or pressing the button. When you are done, select “Quit” from the “BlueJ Virtual Machine” menu to terminate your program. DO NOT just close the window by clicking on the red button in the upper-left corner. Actually quit instead.

What if my program doesnʼt work?

While you are working on this lab, a disturbing thing may happen. Your program may not do what you expect. For example, instead of seeing nice “+OK” messages in your text area it may be filled with “-ERR” messages. There is a technique that may help you identify the source of your problem. Before running your own program, start TCPCapture and configure it to capture all POP packets. Then run your program and look at all the packets that are sent between your client and the server. You will see the actual contents of the messages your program sends along with the “+OK” and “-ERR” message from the server. This should provide you with a better understanding of what is going wrong and enable you to fix the problem.

Submission Instructions

Return to the Finder and look in your Netfiles “Private” folder (or the “Documents” or “Desktop” folder if you saved your project there). You should find the folder that BlueJ created for your project. Its name should be the one you picked for your project (something like YellowLab2). Now click on the blue “Users” disk on the Desktop to open a new Finder window. Navigate to b/blawson/ inbox/cmsc150/lab2. Then drag-and-drop from the first Finder window your project folder into the cmsc150/ lab2 folder in my Netfiles inbox. The deadline for submitting your work is 17:00 on Saturday 13 September (the Saturday immediately following the lab). Jepson G22 will be open per the regular computer lab schedule on Saturdays: 11:00-17:00. If you come to the lab on Saturday and it is not open, one of the student assistants in G28 will open it for you upon request. If you submit prior to this deadline and later discover that your submission was flawed, you can submit again pro- vided the deadline has not passed. We will grade the latest submission made before the 17:00 Saturday deadline. Netfiles will not let you submit again unless you change the name of your folder slightly. It does this to prevent an- other student from accidentally overwriting one of your submissions. Just add something to the folder name (like the word “revised”) and the re-submission should work fine.

Grading

Grading will typically be done by evaluating your lab submission based on three sets of criteria: how complete your code is, how well it works, and how clearly it is written. Completeness measures whether you have done a reason- able job of writing instructions to provide the desired functionality, even if the instructions you wrote don’t fully work. Correctness measures whether all the desired functionality works as we expect it to. Good style is also an important quality for programs to have; it is essential to make programs readable and understandable. Over the course of the semester we will get pickier about style issues, but it is important for you to get into good habits from the very beginning. Completeness (7 points) / Correctness (3 points)

- GUI layout - Connecting to and disconnecting from the server - Logging in - Retrieving mail messages - Displaying mail messages in top text area - Displaying server responses in bottom text area

Style (5 points)

  • Commenting
  • Good variable names
  • Good, consistent indentation
  • Good use of blank lines
  • Removing unused methods

Using a JPasswordField

In this lab, you should use a form of GUI component called a JPasswordField. This is a text field specially de- signed for inputing passwords. Unlike a normal text field, a JPasswordField doesn’t show the characters a user types into the field. Instead it displays a star for each character typed. The interface to a JPasswordField from within your program is a bit different from that for JTextFields. If you invoke the getText method on a JPasswordField, you will get a message telling you that the method is “depre- cated.’’ This means that the getText method may be removed from the Swing library in the future. The message is annoying enough that we would like to provide an alternate way for you to access the contents of a JPassword- Field. In any context where you would like to use an invocation like somePasswordField.getText() you should instead use an expression of the form new String( somePasswordField.getPassword() ) This will have the same effect as invoking getText, but there will be no message about using a deprecated method.

Using a Conditional Statement

There is a construct in Java that makes it fairly easy to revise the dataAvailable method in such a way that the “+OK” responses from the server will not appear in the same text area as the contents of email message you retrieve. It is called an if statement. It allows you to tell Java to choose between executing two sets of instructions in a method based on a specified condition. For example, in your mail program you could use an if statement to enable Java to choose whether a line retrieved within your dataAvailable method should be displayed in the top text area or the bottom text area based on whether the line began with the text “+OK”. An if statement takes the form:

if ( some-condition ) {

statement(s) to execute if condition is true

} else {

statement(s) to execute if condition is false

} For your program, the condition that should determine which text area should be used is whether the latest line re- ceived from the server begins with “+OK” or not. If the name lineFromServer refers to the latest line retrieved from the server, you can express this choice in Java by saying if ( lineFromServer.startsWith( “+OK” ) ) {

statement(s) to execute if condition is true

} else {

statement(s) to execute if condition is false

} The statement(s) included within the first pair of curly braces should append the contents of the line received to the lower text area. Statements to display the line received in the upper text area should be placed between the second pair of curly braces. In order to use this code, you will have to place lines to declare the variable linesFrom- Server, to get the next line from the server, and to associate this line with the variable linesFromServer before the if statement. We want you to modify your POP program so that the final version of your dataAvailable method includes such an if statement. Of course, this code will be fooled by any email message that contains a line that actually starts with +OK” or if the server sends a “-ERR” response. We will worry about how to deal with that issue next week....