您的当前位置:首页正文

协议解析 之 为Outlook使用的 SmtpServer PopServerJ2EE

2023-04-05 来源:九壹网
C 之指针学习.协议解析 之 为Outlook使用的 SmtpServer PopServerJ2EE 2011-01-11 22:49:39 阅读18 评论0 字号:大中小 订阅 .

Server全写为线程类 用Start类来调用它

SmtpStart========================================================

package com.softeem.outlookServer;

import java.io.IOException; import java.net.ServerSocket; import java.net.Socket;

public class OutlookSmtpStart {

public static void main(String[] args) throws IOException {

OutlookSmtpStart t1 = new OutlookSmtpStart(); t1.run();

}

public void run() {

try {

ServerSocket ssk1 = new ServerSocket(25);

while (true) { // 多用户的访问 Socket socket1 = ssk1.accept();

OutlookSmtpServer smtpServer = new OutlookSmtpServer(socket1); smtpServer.start();

}

} catch (Exception e) { e.printStackTrace(); }

} }

SmtpServer==========================================

package com.softeem.outlookServer;

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.ServerSocket; import java.net.Socket; import java.util.Date;

import com.softeem.dao.MailDAO; import com.softeem.dao.UserDAO; import com.softeem.dto.TbMail; import com.softeem.dto.TbUser; /**

* 接收从 outlook 发送的信息 *

* @author Administrator * */

public class OutlookSmtpServer extends Thread {

OutputStream os = null; InputStream is = null; Socket socket;

TbUser user;

TbMail mail = new TbMail();

public OutlookSmtpServer(Socket socket) { this.socket = socket; }

public void initConnect() { try {

System.out.println(\"connection set up!@ \");

os = socket.getOutputStream(); is = socket.getInputStream();

} catch (Exception e) { e.printStackTrace(); } finally {

} }

public void startServer(InputStream is) {

try {

welcome();

String command = \"\";

while (!(command = getCommand(is)).equals(\"\")) {

if (command.startsWith(\"MAIL FROM:\")) { getFROM(command);

} else if (command.startsWith(\"RCPT TO:\")) { getRCPT(command);

} else if (command.startsWith(\"DATA\")) { getDATA(command);

// 我以为data完了之后就是邮件正文了 System.out.println(\"正文开始-- \"); readMail();

} else if (command.startsWith(\"QUIT\")) { responseClient(os, \"221 2.0.0 Bye\"); break; } }

} catch (Exception e) { e.printStackTrace(); }

}

public void welcome() {

responseClient(os, \"220 zwwww03 ESMTP ready\"); System.out.println(\"ehlo-- :\" + getCommand(is));

responseClient(os, \"250 smtp.Java1014.com\");

responseClient(os, \"250-AUTH PLAIN LOGIN\"); responseClient(os, \"250 STARTTLS\");

}

public void getFROM(String command) {

String str = command.split(\"<\")[1].split(\">\")[0]; mail.setMailFrom(str);

user = new UserDAO().findByUserName(str);

responseClient(os, \"250 2.1.0 Ok\"); }

public void getRCPT(String command) {

String str = command.split(\"<\")[1].split(\">\")[0]; mail.setSendTo(str);

responseClient(os, \"250 2.1.0 Ok\"); }

public void getDATA(String command) {

System.out.println(\"data -- \" + command);// data 结束后就开始读取正文 responseClient(os, \"354 End data with .\");

}

public void readMail() {

String str = \"\"; try {

BufferedReader br = new BufferedReader(new InputStreamReader(is));

while (!(str = br.readLine()).equals(\"\")) {// 邮件头和邮件正文之间有一个空行做分割

if (str.startsWith(\"From\") || str.startsWith(\"To\")

|| str.startsWith(\"Subject\") || str.startsWith(\"Date\")) { String key = str.split(\":\")[0]; String value = str.split(\":\")[1];

if (key.equals(\"Subject\")) {

mail.setMailSubject(value); } else if (key.equals(\"Date\")) {

mail.setSendDate(new Date()); } }

System.out.println(\"getHeader: \" + str); }

getText(br); saveMail(mail);

responseClient(os, \"250 2.0.0 Ok: queued as 3C8CF699F8D\");

} catch (Exception e) { e.printStackTrace(); } }

public void getText(BufferedReader br) { try {

StringBuffer mainBody = new StringBuffer(); String str = \"\";

while (!(str = br.readLine()).equals(\".\")) { mainBody.append(str + \"\\n\");

System.out.println(\"mainBody is \" + str); }

mail.setMainBody(mainBody.toString()); mail.setMailSize(mainBody.length());

} catch (IOException e) { e.printStackTrace(); } }

public void saveMail(TbMail mail) {

new MailDAO().sendMail(user, mail); }

public void responseClient(OutputStream os, String str) { PrintWriter pw = new PrintWriter(os); pw.println(str); pw.flush(); }

public String getCommand(InputStream is) {

String str = \"\"; try {

BufferedReader br = new BufferedReader(new InputStreamReader(is)); str = br.readLine();

System.out.println(\"=====cmd is \" + str);

} catch (Exception e) { e.printStackTrace(); }

return str; }

public void run() { initConnect(); startServer(is); } }

//输出的会话

// connection set up!@

// ehlo-- :EHLO WWW92CFB5C4666 // data -- DATA

// 正文开始--

// getHeader: From: \"raku\" // getHeader: To: // getHeader: Subject: hjh

// getHeader: Date: Sat, 1 Jan 2011 21:04:13 +0800

// getHeader: Message-ID: <000a01cba9b4$631e7920$295b6b60$@com> // getHeader: MIME-Version: 1.0

// getHeader: Content-Type: multipart/alternative;

// getHeader: boundary=\"----=_NextPart_000_000B_01CBA9F7.7141B920\" // getHeader: X-Mailer: Microsoft Office Outlook 12.0

// getHeader: Thread-Index: AcupspmRn4OPNwbrTiy4T0bTUz9J0g== // getHeader: Content-Language: zh-cn

// mainBody is 这是一封 MIME 格式的多部分邮件。 // mainBody is

// mainBody is ------=_NextPart_000_000B_01CBA9F7.7141B920 // mainBody is Content-Type: text/plain; // mainBody is charset=\"us-ascii\"

// mainBody is Content-Transfer-Encoding: 7bit // mainBody is

// mainBody is hjjhh // mainBody is // mainBody is l;l; // mainBody is // mainBody is // mainBody is // mainBody is

// mainBody is ------=_NextPart_000_000B_01CBA9F7.7141B920 // mainBody is Content-Type: text/html; // mainBody is charset=\"us-ascii\"

// mainBody is Content-Transfer-Encoding: quoted-printable // mainBody is

// mainBody is // mainBody is xmlns:m=3D\"http://schemas.microsoft.com/office/2004/12/omml\" = // mainBody is xmlns=3D\"http://www.w3.org/TR/REC-html40\"> // mainBody is

// mainBody is

// mainBody is

// mainBody is // content=3D\"Microsoft Word 12 (filtered medium)\"> // mainBody is

// mainBody is // mainBody is // mainBody is

// mainBody is // mainBody is

// mainBody is

// mainBody is

// mainBody is

hjjhh

// mainBody is

// mainBody is

l;l;

// mainBody is

// mainBody is

 

// mainBody is

// mainBody is

// mainBody is

// mainBody is // mainBody is

// mainBody is // mainBody is

// mainBody is ------=_NextPart_000_000B_01CBA9F7.7141B920-- // mainBody is

popStart===================================

package com.softeem.outlookServer;

import java.io.IOException; import java.net.ServerSocket; import java.net.Socket;

public class OutlookPopStart {

public static void main(String[] args) throws IOException {

OutlookPopStart t1 = new OutlookPopStart(); t1.run();

}

public void run() {

try {

ServerSocket ssk2 = new ServerSocket(110);

while (true) {

Socket socket2 = ssk2.accept();

OutlookPopServer popServer=new OutlookPopServer(socket2); popServer.start(); }

} catch (Exception e) { e.printStackTrace(); }

} }

PopServer===================================

package com.softeem.outlookServer;

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.util.List;

import com.softeem.dao.MailDAO; import com.softeem.dao.UserDAO; import com.softeem.dto.TbMail; import com.softeem.dto.TbUser;

public class OutlookPopServer extends Thread {

Socket socket;

InputStream is; OutputStream os;

String sendMan; String receiveMan;

TbUser user;

List mails;// 暂存查询到的邮件

public OutlookPopServer(Socket socket) { this.socket = socket; }

public void initStream() { try {

System.out.println(\"connection set up!@ \");

is = socket.getInputStream(); os = socket.getOutputStream();

} catch (Exception e) { e.printStackTrace(); } }

public static void responseClient(OutputStream os, String str) { PrintWriter pw = new PrintWriter(os); pw.println(str); pw.flush(); }

public static String getCommand(InputStream is) { String str = \"\"; try {

InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); str = br.readLine();

System.out.println(\"------cmd is \" + str); } catch (IOException e) { e.printStackTrace(); }

return str; }

public boolean connectServer() {

boolean b = false;

// 第一句应该是由服务器发出 responseClient(os,

\"+OK Welcome to coremail Mail Pop3 Server <163com[20050206]>\");

return true; }

public boolean auth() {

responseClient(os, \"-Err \"); return true; }

public void login(String command) { boolean b = false;

String username = command.split(\" \")[1]; responseClient(os, \"+OK username has get\");

command = getCommand(is);

System.out.println(command); // 期待这里返回 pass password

if (command.startsWith(\"PASS \")) {

String psw = command.split(\" \")[1];

UserDAO userDAO = new UserDAO();

user = userDAO.findByUserName(username); if (user != null) {

if (user.getPsw().equals(psw)) { b = true; } } }

if (b) {

responseClient(os, \"+OK 2 messages log ok\"); } else {

responseClient(os, \"-ERR authorization failed\"); }

}

public void getStat(String command) {

MailDAO mailDAO = new MailDAO();

int countMail = mailDAO.countMails(user, 1); int sumSize = mailDAO.sumSize(user, 1);

responseClient(os, \"+OK \" + countMail + \" \" + sumSize); }

public void getUIDL() {

responseClient(os, \"+OK 1 1tbisBsHaEX9byI9EQAAsd \");// 邮件的唯一标识符 responseClient(os, \".\"); }

public void listMails(String command) { responseClient(os, \"+OK list \");

MailDAO mailDAO = new MailDAO(); mails = mailDAO.findMails(user, 1);

int i = 0; int size = 0;

for (TbMail mail : mails) { i++;

size = mail.getMailSize();

responseClient(os, String.valueOf(i) + \" \" + size); }

responseClient(os, \".\");// 以一个.结束 }

public void getTop() {

responseClient(os, \"+OK get TOP? \");// 之后仍然在阻塞中 responseClient(os, \".\");// 以一个.结束 }

public void recMail(String command) {

String number = command.split(\" \")[1]; TbMail mail = mails.get(0);

responseClient(os, \"+OK \");

// 返回邮件全部内容 最后以 一个 . 结尾

responseClient(os, \"Return-Path: <\" + mail.getMailFrom() + \">\"); responseClient(os, \"Delivered-To: \" + mail.getSendTo());

responseClient(os, \"Received: \" + mail.getSendDate()); responseClient(os, \"\");// 空行之后就是邮件开始

responseClient(os, \"From: \" + mail.getMailFrom()); responseClient(os, \"To: \" + mail.getSendTo()); responseClient(os, \"Date: \" + mail.getSendTo());

responseClient(os, \"Subject: \" + mail.getMailSubject());

// 再来一个空行 邮件正文开始

String mainBody = mail.getMainBody(); String[] aText = mainBody.split(\"\\n\"); for (String str : aText) {

responseClient(os, str); }

responseClient(os, \".\");

}

public void dele(String command) {

int number = Integer.parseInt(command.split(\" \")[1]); if(number>mails.size()){ }else{

TbMail mail = mails.get(number-1);

// 在数据库中将邮箱 标志位已读.... new MailDAO().move(user, mail, 2);

System.out.println(\"outlook pop mail to has read \"+mail.getMailId()); responseClient(os, \"+OK \"); } }

public void startServer() { boolean b = true;

b = connectServer();

String command = \"\"; if (b) {

while (!(command = getCommand(is)).equals(\"\")) {

if (command.startsWith(\"AUTH\")) { auth();

} else if (command.startsWith(\"USER\")) { login(command);

} else if (command.startsWith(\"STAT\")) { getStat(command);

} else if (command.startsWith(\"UIDL\")) { getUIDL();

} else if (command.startsWith(\"LIST\")) { listMails(command);

} else if (command.startsWith(\"TOP\")) { getTop();

} else if (command.startsWith(\"RETR\")) { recMail(command);

} else if (command.startsWith(\"DELE\")) { dele(command);

} else if (command.startsWith(\"QUIT\")) {

responseClient(os, \"+OK\"); break; } } }

}

public void run() {

initStream(); startServer();

} }

// 直接发 一个 +OK 给 Outlook 返回了一个auth

// +OK Welcome to coremail Mail Pop3 Server <163com[20050206]> // command is AUTH // AUTH null // -Err

// command is USER haha // USER haha // +OK core mail

// command is PASS 123 // PASS 123 // +OK

// command is STAT // STAT null // +OK 3 333

// command is LIST // LIST null

缺陷: SMtp只能是 outlook 2007使用 pop只能2003 使用 评论这张 转发至微博

0人 | 分享到: 阅读(18)| 评论(0)| 引用 (0) |举报 .

因篇幅问题不能全部显示,请点此查看更多更全内容