Mail Retrieval: Using the POP3 Client
Example 24 shows basic POP3 command access. After making a successful connection to a POP3 server and completing login transactions, the program checks for messages in a mail box. If it finds any messages, it gets the first message in the mail drop and displays it on the screen.
NOTE: Servers and files shown in the code might not exist and are included as examples only.
Example 24 – Using the POP3 client
RWPop3Reply reply;
RWPop3StatReply statReply;
RWPop3ConnReply connReply;
 
try {
RWPop3Client client; // 1
connReply = client.connect("mail.roguewave.com"); // 2
reply = client.user("user"); // 3
reply = client.pass("password"); // 4
 
 
statReply = client.stat(); // 5
 
int totalMsgs = statReply.getMessageCount(); // 6
cout << "The number of messages is : " << totalMsgs << endl;
 
if (totalMsgs > 0) {
cout << "Let's retrieve the first message." << endl;
dataReply = client.retr(1); // 7
if (!dataReply.isErr()) { // 8
RWSocketPortal portal = dataReply.portal(); // 9
RWPortalIStream istr(portal); //10
RWCString line;
 
do {
line.readLine(istr); //11
line = rwNormalizeLine(line); //12
 
if (line != ".") {
cout << line << endl;
}
} while (line != "."); //13
}
}
}
catch (const RWxmsg& msg) { //14
cout << "ERROR: " << msg.why() << endl;
}
//1 Constructs an RWPop3Client object. The object is set to an unconnected state that is ready to use the connect() method of RWPop3Client to establish a connection with a POP3 server. When the object goes out of scope, disconnection from the server is automatic.
//2 Establishes a connection with a POP3 server.
//3 Performs the USER protocol command.
//4 Performs the PASS protocol command.
//5 Performs the STAT protocol command.
//6 Gets the total number of pending messages in the mail drop using the getMessageCount() method of class RWPop3StatReply.
//7 Performs the RETR protocol command and immediately redeems the result as the RWPop3DataReply object dataReply. dataReply contains a socket portal to complete the data portion of the protocol transfer.
//8 Checks that the data reply is +OK. A successful reply from the retr() method is normally +OK, which indicates that the data connection has been opened.
//9 Gets the data socket portal from the dataReply object to retrieve data from the underlying socket.
//10 Creates an RWPortalIStream object istr from the socket portal in //9. The object istr becomes the data source.
//11 Reads one line of the mail data at a time.
//12 Removes the line feed at the end of the line.
//13 Reads message data until a period is in a line by itself, which indicates the end of a mail message.
//14 This catch clause catches all POP3 package, Networking package, and Threads Module exceptions that can be thrown from within the try block because all Rogue Wave exceptions are derived from RWxmsg.
This example demonstrates one method of redeeming an RWTIOUResult object. For other methods, see Multithreading and IOUs and File Retrieval: Using the FTP Agent (Part II).