Only sending and receiving are complete.
It is written in two parts:
I. Introduction to POP3 commands (copied); II. instances.
I. Introduction to POP3 commands
Telnet 119.119.119.212 110 ------------------------------- use the Telnet command to connect to port 110 of the server
Trying 119.119.119.212... -------------------------------- connecting to port 110 of the server
Connected to 119.119.119.212. ------------------------ connection to port 110 of the server is successful.
+ OK winmail Mail Server POP3 ready
User Username ------------------------------------------ enter the user name. username is the specific user name.
+ OK ---------------------------------------------------- the command is successfully executed.
Pass password ------------------------------------------ enter the user password. The password is the specific password.
+ OK 2 messages ----------------------------------------- pass password authentication
(-Err authorization failed ----------------------------- password authentication failed)
Stat ----------------------------------------------------- mailbox status
+ OK 2 6415 --------------------------------------------- 2 indicates the total number of mails in the mailbox, and 6415 indicates the total number of bytes.
List --------------------------------------------------- list the bytes of each email
+ OK -------------------------------------------------- the command is successfully executed and displayed. The mail serial number is displayed on the left and the mail size is displayed on the right.
1 537 -------------------------------------------------- 1st emails in 537 bytes
2 5878 ------------------------------------------------- 2nd emails in 5878 bytes
.
Top 1 -------------------------------------------------- receive 1st emails
+ OK ---------------------------------------------------- received successfully, 1st mail headers are returned
Return-path: <test1@look.com>
Delivered-to: test2@look.com
Received: (winmail server invoked for SMTP delivery); Mon, 25 Oct 2004 14:24:27 + 0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 + 0800
Subject: Test mail
.
RETR 1 --------------------------------------------------- receive 1st emails
+ OK ---------------------------------------------------- the message is received successfully, and all content of the 1st emails is returned.
Return-path: <test1@look.com>
Delivered-to: test2@look.com
Received: (winmail server invoked for SMTP delivery); Mon, 25 Oct 2004 14:24:27 + 0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 + 0800
Subject: Test mail
Hi, Test2
This is a test mail, you don't reply it.
.
Dele 1 --------------------------------------------------- Delete 1st emails
+ OK ---------------------------------------------------- deleted successfully
Dele 2 --------------------------------------------------- Delete 2nd emails
+ OK ---------------------------------------------------- deleted successfully
Quit --------------------------------------------------- end the session
+ OK ---------------------------------------------------- the command is successfully executed.
Ii. Mail sending instance
Package mail;
Import java. Io. bufferedreader;
Import java. Io. bufferedwriter;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Io. outputstreamwriter;
Import java.net. Socket;
Import java.net. unknownhostexception;
Import java. util. stringtokenizer;
Public class pop3client {
Private Socket socket;
Private Boolean DEBUG = true;
Public static void main (string [] ARGs) throws unknownhostexception, ioexception {
String Server = "pop3.126.com"; // POP3 server address
String user = "wasingmon"; // User Name
String Password = ""; // Password
Pop3client pop3client = new pop3client (server, 110 );
Pop3client. recievemail (user, password );
}
Public pop3client (string server, int port) throws unknownhostexception, ioexception {
Try {
Socket = new socket (server, Port );
} Catch (exception e ){
E. printstacktrace ();
} Finally {
System. Out. println ("establish a connection! ");
}
}
// Get a line of commands returned by the server
Public String getreturn (bufferedreader in ){
String line = "";
Try {
Line = in. Readline ();
If (Debug ){
System. Out. println ("server return status:" + line );
}
} Catch (exception e ){
E. printstacktrace ();
}
Return line;
}
// Obtain the first field from the returned command, that is, the server's return status code (+ OK or-err)
Public String getresult (string line ){
Stringtokenizer ST = new stringtokenizer (line ,"");
Return st. nexttoken ();
}
// Send command
Private string sendserver (string STR, bufferedreader in, bufferedwriter out) throws ioexception {
Out. Write (STR );
Out. newline ();
Out. Flush ();
If (Debug)
{
System. Out. println ("sent command:" + Str );
}
Return getreturn (in );
}
// USER command
Public void user (string user, bufferedreader in, bufferedwriter out) throws ioexception {
String result;
Result = getresult (getreturn (in ));
If (! "+ OK". Equals (result )){
Throw new ioexception ("failed to connect to the server! ");
}
Result = getresult (sendserver ("user" + user, in, out ));
If (! "+ OK". Equals (result )){
Throw new ioexception ("the user name is incorrect! ");
}
}
// PASS Command
Public void pass (string password, bufferedreader in, bufferedwriter out) throws ioexception {
String result;
Result = getresult (sendserver ("pass" + password, in, out ));
If (! "+ OK". Equals (result )){
Throw new ioexception ("Incorrect password! ");
}
}
// STAT command
Public int Stat (bufferedreader in, bufferedwriter out) throws ioexception {
String result;
String line;
Int mailnum;
Line = sendserver ("stat", in, out );
Stringtokenizer ST = new stringtokenizer (line ,"");
Result = ST. nexttoken ();
If (St. hasmoretokens ())
Mailnum = integer. parseint (St. nexttoken ());
Else
Mailnum = 0;
If (! "+ OK". Equals (result )){
Throw new ioexception ("An error occurred while viewing the mailbox status! ");
}
System. Out. println ("Total emails" + mailnum + "mails ");
Return mailnum;
}
// Obtain the email details
Public String getmessagedetail (bufferedreader in ){
String message = "";
String line;
Try {
Line = in. Readline ();
While (! ".". Inclusignorecase (line )){
Message = message + LINE + "/N ";
Line = in. Readline ();
}
} Catch (exception e ){
E. printstacktrace ();
}
Return message;
}
// RETR command
Public void RETR (INT mailnum, bufferedreader in, bufferedwriter out) throws ioexception {
String result;
For (INT I = 1; I <= mailnum; I ++ ){
Result = getresult (sendserver ("retr" + I, in, out ));
If (! "+ OK". Equals (result )){
Throw new ioexception ("An error occurred while receiving emails! ");
}
System. Out. println ("th" + I + "");
System. Out. println (getmessagedetail (in ));
}
}
// Exit
Public void quit (bufferedreader in, bufferedwriter out) throws ioexception {
String result;
Result = getresult (sendserver ("quit", in, out ));
If (! "+ OK". Equals (result )){
Throw new ioexception ("failed to exit correctly ");
}
}
// Receiving email program
Public Boolean recievemail (string user, string password ){
Try {
Bufferedreader in = new bufferedreader (New inputstreamreader (socket. getinputstream ()));
Bufferedwriter out = new bufferedwriter (New outputstreamwriter (socket. getoutputstream ()));
User (user, in, out );
Pass (password, in, out );
Int mailnum;
Mailnum = Stat (In, out );
RETR (mailnum, in, out );
Quit (In, out );
} Catch (exception e ){
E. printstacktrace ();
Return false;
}
Return true;
}
}