这里只做简单的接收解析邮件发送请求,后续会在这个基础上做深入探索。
发送组件:
/**
* @author: Nemo
* @date: 2018/9/28.
*/
public class MainSender {
public static void send() {
String sender = "sender@link-nemo.com";
String receiver = "reciver@link-nemo.com";
String password = "xxxxxx";
String user = new BASE64Encoder().encode(sender.getBytes());
String pass = new BASE64Encoder().encode(password.getBytes());
try {
//邮箱服务的地址,端口(smtp服务)
Socket socket = new Socket("10.1.10.37", 8086);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//
PrintWriter writter = new PrintWriter(outputStream, true);
System.out.println(reader.readLine());
//Hello
writter.println("HELO Nemo");
System.out.println(reader.readLine());
//AUTH LOGIN
writter.println("auth login");
System.out.println(reader.readLine());
writter.println(user);
System.out.println(reader.readLine());
writter.println(pass);
System.out.println(reader.readLine());
//Above Authentication successful
//Set mail from and rcpt to
writter.println("mail from:<" + sender + ">");
System.out.println(reader.readLine());
writter.println("rcpt to:<" + receiver + ">");
System.out.println(reader.readLine());
//Set data
writter.println("data");
System.out.println(reader.readLine());
//title
writter.println("subject:你好Nemo,这里是标题");
writter.println("from:" + sender);
writter.println("to:" + receiver);
writter.println("Content-Type: text/plain;charset=\"utf-8\"");
writter.println();
//content
writter.println("你好Nemo,这里是主体内容?");
writter.println(".");
writter.println("");
System.out.println(reader.readLine());
//Say GoodBye
writter.println("rset");
System.out.println(reader.readLine());
writter.println("quit");
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]){
send();
}
}
接收组件:
/**
* @author: Nemo
* @date: 2018/9/28.
*/
public class MainServerTest {
/**
* 入口主函数
* @param args
*/
public static void main(String[] args) {
MainServerTest server = new MainServerTest();
server.start();
}
/**
* 启动服务器,并接收用户请求进行处理
*/
private void start() {
ServerSocket serverSocket = null;
try {
//打开端口访问
serverSocket = new ServerSocket(8086, 1, InetAddress.getByName("10.1.10.37"));
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
//开始处理请求
dealRequest(serverSocket);
}
/**
* 开始处理请求
* @param serverSocket
*/
private void dealRequest(ServerSocket serverSocket){
while(true) {
try {
//创建socket,等待请求
Socket socket = serverSocket.accept();
//如果有请求,则直接放到线程中直接处理即可
Thread thread = new Thread(new DealRequestThread(socket));
thread.start();
} catch (IOException e) {
e.printStackTrace();
//异常不终止所有进程,应立即继续下一个请求
continue;
}
}
}
}
class DealRequestThread implements Runnable{
private Socket socket;
public DealRequestThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
Thread current = Thread.currentThread();
System.out.println("当前处理线程:" + current.getName());
//本次请求的输入流
InputStream input = socket.getInputStream();
//对于本次请求的输出流
OutputStream output = socket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
PrintWriter writter = new PrintWriter(output, true);
//告诉对方会话开始
writter.println("220 Hello there,this is Nemo mail.");
//第一行,回应对方的打招呼
String line = reader.readLine();
System.out.println(line);
writter.println("250 Copy that.");
//第二行,对方开始请求验证登录,提示对方输入用户名
line = reader.readLine();
System.out.println(line);
writter.println("334 Username:");
//第三行,对方已输入用户名,提示对方输入密码
line = reader.readLine();
System.out.println(line);
writter.println("334 Password:");
//第四行,对方已输入密码,开始验证对方用户名密码是否成功
line = reader.readLine();
System.out.println(line);
//TODO 校验
writter.println("235 Authentication successful");
//第五行,接收对方发送账号
line = reader.readLine();
System.out.println(line);
writter.println("250 Mail Ok");
//第六行,接收对方接收账号
line = reader.readLine();
System.out.println(line);
writter.println("250 Rcpt Ok");
//第七行,对方发送data标记主体内容开始,这里需要提示对方结束标记
line = reader.readLine();
System.out.println(line);
writter.println("354 End data with .");
//开始接受主体内容
//TODO 把内容存到库中,准备下行投递发送
while (!".".equals(line = reader.readLine())) {
System.out.println("主体内容:"+line);
}
reader.readLine();
writter.println("250 Data Ok:queued as freedom");
//结尾:
line = reader.readLine();
System.out.println(line);
writter.println("250 Reset Ok");
//结束会话
line = reader.readLine();
System.out.println(line);
writter.println("250 Bye");
socket.close();
} catch (IOException e) {
//错误只做异常堆栈输出,不向外抛出
e.printStackTrace();
}
}
}