?? pop3demo.java
字號:
import java.io.*;
import java.net.*;
/**
* <p>Title: SMTP協(xié)議接收郵件</p>
* <p>Description: 通過Socket連接POP3服務器,使用SMTP協(xié)議接收郵件服務器中的郵件</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: </p>
* @author 杜江
* @version 1.0
*/
class POP3Demo
{
/**
*<br>方法說明:主方法,接收用戶輸入
*<br>輸入?yún)?shù):
*<br>返回類型:
*/
public static void main(String[] args){
if(args.length!=3){
System.out.println("USE: java POP3Demo mailhost user password");
}
new POP3Demo().receive(args[0],args[1],args[2]);
}
/**
*<br>方法說明:接收郵件
*<br>輸入?yún)?shù):String popServer 服務器地址
*<br>輸入?yún)?shù):String popUser 郵箱用戶名
*<br>輸入?yún)?shù):String popPassword 郵箱密碼
*<br>返回類型:
*/
public static void receive (String popServer, String popUser, String popPassword)
{
String POP3Server = popServer;
int POP3Port = 110;
Socket client = null;
try
{
// 創(chuàng)建一個連接到POP3服務程序的套接字。
client = new Socket (POP3Server, POP3Port);
//創(chuàng)建一個BufferedReader對象,以便從套接字讀取輸出。
InputStream is = client.getInputStream ();
BufferedReader sockin;
sockin = new BufferedReader (new InputStreamReader (is));
//創(chuàng)建一個PrintWriter對象,以便向套接字寫入內容。
OutputStream os = client.getOutputStream ();
PrintWriter sockout;
sockout = new PrintWriter (os, true); // true for auto-flush
// 顯示POP3握手信息。
System.out.println ("S:" + sockin.readLine ());
/*-- 與POP3服務器握手過程 --*/
System.out.print ("C:");
String cmd = "user "+popUser;
// 將用戶名發(fā)送到POP3服務程序。
System.out.println (cmd);
sockout.println (cmd);
// 讀取POP3服務程序的回應消息。
String reply = sockin.readLine ();
System.out.println ("S:" + reply);
System.out.print ("C:");
cmd = "pass ";
// 將密碼發(fā)送到POP3服務程序。
System.out.println(cmd+"*********");
sockout.println (cmd+popPassword);
// 讀取POP3服務程序的回應消息。
reply = sockin.readLine ();
System.out.println ("S:" + reply);
System.out.print ("C:");
cmd = "stat";
// 獲取郵件數(shù)據(jù)。
System.out.println(cmd);
sockout.println (cmd);
// 讀取POP3服務程序的回應消息。
reply = sockin.readLine ();
System.out.println ("S:" + reply);
if(reply==null) return;
System.out.print ("C:");
cmd = "retr 1";
// 將接收第一豐郵件命令發(fā)送到POP3服務程序。
System.out.println(cmd);
sockout.println (cmd);
// 輸入了RETR命令并且返回了成功的回應碼,持續(xù)從套接字讀取輸出,
// 直到遇到<CRLF>.<CRLF>。這時從套接字讀出的輸出就是郵件的內容。
if (cmd.toLowerCase ().startsWith ("retr") &&
reply.charAt (0) == '+')
do
{
reply = sockin.readLine ();
System.out.println ("S:" + reply);
if (reply != null && reply.length () > 0)
if (reply.charAt (0) == '.')
break;
}
while (true);
cmd = "quit";
// 將命令發(fā)送到POP3服務程序。
System.out.print (cmd);
sockout.println (cmd);
}
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
try
{ if (client != null)
client.close ();
}
catch (IOException e)
{
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -