package com.huawei.xbliuc.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class POP3Demo
{
private static String POP3Service = "pop.126.com";
private static String USERNAME = "username";
private static String PASSWORD = "password";
public static void main(String[] args)
{
get();
}
public static void get()
{
int POP3Port = 110;
Socket client = null;
try
{
//向POP3服务程序建立一个套接字连接
client = new Socket(POP3Service, POP3Port);
//创建一个BufferedReader对象,一遍从套接字中读取输出
InputStream is = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//创建一个PrintWriter对象,以便向套接字中写入内容
OutputStream os = client.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
//显示通SMTP服务程序握手的过程
System.out.println("S:" + br.readLine());
System.out.println("UserName:" + POP3Demo.USERNAME);
System.out.println("S:" + br.readLine());
System.out.println("PassWord:" + POP3Demo.PASSWORD);
System.out.println("S:" + br.readLine());
pw.print("start");
String[] temp = br.readLine().split(" ");
int count = Integer.parseInt(temp[1]);//得到信箱中有多少封信件
//依次打印邮件的内容
for (int i = 1; i < count + 1; i++)
{
pw.print("retr" + i);
System.out.println("以下为第" + i + "封邮件的内容");
while (true)
{
String readLine = br.readLine();
System.out.println(readLine);
if (readLine.toLowerCase().equals("."))
{
break;
}
}
}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (client != null)
{
try
{
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class POP3Demo
{
private static String POP3Service = "pop.126.com";
private static String USERNAME = "username";
private static String PASSWORD = "password";
public static void main(String[] args)
{
get();
}
public static void get()
{
int POP3Port = 110;
Socket client = null;
try
{
//向POP3服务程序建立一个套接字连接
client = new Socket(POP3Service, POP3Port);
//创建一个BufferedReader对象,一遍从套接字中读取输出
InputStream is = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//创建一个PrintWriter对象,以便向套接字中写入内容
OutputStream os = client.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
//显示通SMTP服务程序握手的过程
System.out.println("S:" + br.readLine());
System.out.println("UserName:" + POP3Demo.USERNAME);
System.out.println("S:" + br.readLine());
System.out.println("PassWord:" + POP3Demo.PASSWORD);
System.out.println("S:" + br.readLine());
pw.print("start");
String[] temp = br.readLine().split(" ");
int count = Integer.parseInt(temp[1]);//得到信箱中有多少封信件
//依次打印邮件的内容
for (int i = 1; i < count + 1; i++)
{
pw.print("retr" + i);
System.out.println("以下为第" + i + "封邮件的内容");
while (true)
{
String readLine = br.readLine();
System.out.println(readLine);
if (readLine.toLowerCase().equals("."))
{
break;
}
}
}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (client != null)
{
try
{
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}