Xãy dửùng POP3 Server

Một phần của tài liệu Giao thức truyền thông trên Internet (Trang 73 - 93)

2. 3.1 Caực khaựi nieọm cụ baỷn

2.2.2.2.Xãy dửùng POP3 Server

Trỡnh POP3 Server gồm hai phần, phần POPServer chũu traựch nhieọm mụỷ socket vaứ laộng nghe keỏt noỏi tửứ trỡnh khaựch gụỷi lẽn theo coồng 110 (coồng maởc ủũnh cuỷa giao thửực POP3). Khi nhaọn ủửụùc keỏt noỏi, POPServer yẽu cầu lụựp PÔPCnnection phãn tớch caực leọnh cuỷa giao thửực POP3 vaứ gụỷi mail về cho trỡnh khaựch. Lụựp POP3 ủửụùc caứi ủaởt nhử sau:

POPServer.java

import java.io.*; import java.net.*; import java.util.*;

public class POPServer extends Thread { public Vector connections;

public Boolean stopRequested; protected ServerSocket listenSocket; public POPServer() throws Exception {

int port = 0;

String portString = null;

// Laỏy soự hieọu coồng tửứ file caỏu hỡnh try {

portString = Server.properties.getProperty("pop.port"); port = Integer.parseInt(portString);

} catch (NumberFormatException e) {

throw new Exception("Invalid 'pop.port' - " + portString); }

// Laộng nghe trẽn socket sửù keỏt noỏi tửứ trỡnh khaựch listenSocket = new ServerSocket(port); // Táo maỷng chửựa danh saựch caực keỏt noỏi connections = new Vector(10, 10); }

public void removeConnection(POPConnection connection) { }

/**

* Xửỷ lyự keỏt noỏi */

public void run() {

// Laởp võ taọn cho ủeỏn khi coự yẽu cầu dửứng stopRequested = new Boolean(false); while (true) {

synchronized (stopRequested) {

if (stopRequested.booleanValue()) break;

}

try {

Socket s = listenSocket.accept();

//Yẽu cầu lụựp POPConnection xửỷ lyự giao thửực phúc vú trỡnh khaựch POPConnection connection = new POPConnection(s, this);

connections.addElement(connection); connection.setDaemon(true); connection.start(); } catch (IOException e) { e.printStackTrace(); break; } }

// ẹoựng keỏt noỏi try { listenSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }

Lụựp POPConnection.java chũu traựch nhieọm chớnh trong vieọc xửỷ lyự caực leọnh cuỷa giao thửực POP3. Lụựp POPConnection ủửụùc caứi ủaởt nhử sau :

POPConnection.java

import java.io.*; import java.net.*; import java.util.*;

public class POPConnection extends Thread { protected static final int ENTER_USER = 0;

protected static final int ENTER_PASSWORD = 1; protected static final int TRANSACTION = 2; protected static final int UPDATE = 3;

protected POPServer server; protected Socket socket; protected BufferedReader in; protected PrintStream out; (adsbygoogle = window.adsbygoogle || []).push({});

protected int state;

protected String userName; protected User user;

protected Vector messages = null;

public POPConnection(Socket socket, POPServer server) { super();

this.socket = socket; this.server = server; }

protected void close() { try {

socket.close(); } catch (Exception e) {

System.err.println("Exception trying to close POPConnection socket!"); e.printStackTrace(System.err);

} }

// ẹeỏm soỏ mail coự trong hoọp thử protected int countMessages() {

if (messages == null) return 0;

Enumeration enum = messages.elements(); int count = 0;

while (enum.hasMoreElements()) {

Message message = (Message)enum.nextElement(); if (!message.isDeleted()) count++; } return count; } /**

* Laỏy noọi dung mail dửùa vaứo soỏ thửự tửù cuỷa mail lửu trong hoọp thử */

protected Message getMessage(int number) { if (number <= 0 || number > messages.size())

Message message = (Message)messages.elementAt(number - 1); if (message.isDeleted())

return null; return message; }

protected Message getMessage(String messageNumber) { int number; try { number = Integer.parseInt(messageNumber); } catch (NumberFormatException e) { return null; } return getMessage(number); }

protected long getMessagesSize() { if (messages == null)

return 0;

Enumeration enum = messages.elements(); long size = 0;

while (enum.hasMoreElements()) {

Message message = (Message)enum.nextElement(); if (!message.isDeleted())

size += message.getSize(); }

return size; }

protected void processDELE(StringTokenizer arguments) { if (!arguments.hasMoreTokens()) {

out.println("-ERR must supply message number"); } else {

Message message = getMessage(arguments.nextToken()); if (message == null) {

out.println("-ERR no such message"); } (adsbygoogle = window.adsbygoogle || []).push({});

message.setDeleted(true); out.println("+OK");

} }

/**

* Xửỷ lyự leọnh PASS kieồm tra password */

protected void processEnterPassword(String command, StringTokenizer arguments) {

if (command.equalsIgnoreCase("QUIT")) { stopRequested = true;

out.println("+OK Signing off");

} else if (command.equalsIgnoreCase("PASS")) { if (!arguments.hasMoreTokens()) {

out.println("-ERR must supply password"); return;

}

String password = arguments.nextToken(); if (arguments.hasMoreTokens()) {

out.println("-ERR only one argument to PASS, your password"); return;

}

// Kieồm tra quyền ủaờng nhaọp

user = Server.storage.login(userName, password); if (user == null) {

out.println("-ERR invalid user or password"); state = ENTER_USER;

} else {

// user ủaừ ủaờng nhaọp – traỷ về danh saựch caực mail coự trong hoọp thử messages = Server.storage.getMessages(user);

out.println("+OK mailbox open, " + countMessages() + " messages"); state = TRANSACTION;

} } else {

out.println("-ERR Only use PASS or QUIT commands"); }

} /**

* Xửỷ lyự leọnh USER */

protected void processEnterUser(String command, StringTokenizer arguments) {

stopRequested = true;

out.println("+OK Signing off"); return;

}

if (command.equalsIgnoreCase("USER")) { if (!arguments.hasMoreTokens()) {

out.println("-ERR must supply user name"); return;

} (adsbygoogle = window.adsbygoogle || []).push({});

userName = arguments.nextToken(); if (arguments.hasMoreTokens()) {

out.println("-ERR only one argument to USER, the user name"); return;

}

state = ENTER_PASSWORD;

out.println("+OK use PASS command to send password"); return;

}

out.println("-ERR Only use USER or QUIT commands"); }

/**

* Xửỷ lyự leọnh LIST */

protected void processLIST(StringTokenizer arguments) { if (!arguments.hasMoreTokens()) {

out.println("+OK " + countMessages() + " " + getMessagesSize()); for (int i = 1; i <= messages.size(); i++) {

Message message = getMessage(i); if (message != null) { out.println(i + " " + message.getSize()); } } out.println("."); } else {

String messageNumber = arguments.nextToken(); Message message = getMessage(messageNumber); if (message == null) {

out.println("-ERR no such message"); } else {

out.println("+OK " + messageNumber + " " + message.getSize()); }

} }

/**

* Xửỷ lyự leọnh NOOP – Leọnh NOOP cuỷa POP3 khõng laứm gỡ caỷ, múc ủớch chổ ủeồ trỡnh khaựch xem keỏt noỏi coứn hieọu lửùc hay khõng

*/

protected void processNOOP(StringTokenizer arguments) { out.println("+OK");

} /**

* Xửỷ lyự leọnh QUIT */

protected void processQUIT(StringTokenizer arguments) { Enumeration enum = messages.elements();

while (enum.hasMoreElements()) {

Message message = (Message)enum.nextElement(); if (message.isDeleted()) {

Server.storage.deleteMessage(message); }

}

out.println("+OK Goodbye, " + user.getName()); stopRequested = true;

} /**

* Xửỷ lyự leọnh RETR traỷ về noọi dung mail */ (adsbygoogle = window.adsbygoogle || []).push({});

protected void processRETR(StringTokenizer arguments) { if (!arguments.hasMoreTokens()) {

out.println("-ERR message number required, RETR 1"); } else {

String messageNumber = arguments.nextToken(); Message message = getMessage(messageNumber); if (message == null) {

out.println("-ERR no such message"); return;

out.println("+OK " + message.getSize() + " octets"); // ẹóc dửừ lieọu mail

StringBuffer buffer = Server.storage.getMessageData(message); BufferedReader reader = new BufferedReader(new StringReader(buffer.toString()));

// ẹóc tửứng doứng boolean done = false; try {

while (reader.ready() && (!done)) { String line = reader.readLine(); if (line == null)

break; // Daỏu chaỏm keỏt thuực

if (line.length() >= 1) {

if (line.substring(0, 1).equals(".")) line = "." + line;

} // Gửỷi dửừ lieọu về trỡnh khaựch

out.println(line); } } catch (IOException e) { System.err.println("POPConnection.processRETR()"); e.printStackTrace(System.err); } try { reader.close(); } catch (IOException e) { System.err.println("POPConnection.processRETR() - reader.close()"); e.printStackTrace(System.err); } out.println("."); } } /**

*/

protected void processRSET(StringTokenizer arguments) { Enumeration enum = messages.elements();

while (enum.hasMoreElements()) {

Message message = (Message)enum.nextElement(); if (message.isDeleted()) message.setDeleted(false); } out.println("+OK"); } /**

* Xửỷ lyự leọnh STAT */

protected void processSTAT(StringTokenizer arguments) {

out.println("+OK " + countMessages() + " " + getMessagesSize()); }

/**

* Xửỷ lyự leọnh TOP – traỷ về trỡnh khaựch caực mail header */

protected void processTOP(StringTokenizer arguments) { if (!arguments.hasMoreTokens()) {

out.println("-ERR syntax: TOP <msg> <lines>"); return;

}

String messageNumber = arguments.nextToken(); if (!arguments.hasMoreTokens()) {

out.println("-ERR syntax: TOP <msg> <lines>"); return; } int lines = 0; try { lines = Integer.parseInt(arguments.nextToken()); } catch (NumberFormatException e) {

out.println("-ERR bad number of lines"); return;

}

if (message == null) { (adsbygoogle = window.adsbygoogle || []).push({});

out.println("-ERR no such message"); return;

}

out.println("+OK " + message.getSize() + " octets");

StringBuffer buffer = Server.storage.getMessageData(message); BufferedReader reader = new BufferedReader(new StringReader(buffer.toString()));

boolean done = false; boolean inBody = false; int count = 0;

try {

while (reader.ready() && (!done) && (count <= lines)) { String line = reader.readLine();

if (line == null) break; if (line.length() >= 1) { if (line.substring(0, 1).equals(".")) line = "." + line; } else { inBody = true; } if (inBody) { count++; } out.println(line); } } catch (IOException e) { System.err.println("POPConnection.processTOP()"); e.printStackTrace(System.err); } try {

reader.close(); } catch (IOException e) { System.err.println("POPConnection.processTOP() - reader.close()"); e.printStackTrace(System.err); } out.println("."); } /**

* Xửỷ lyự caực leọnh cuỷa giao thửực POP3 */

protected void processTransaction(String command, StringTokenizer arguments) { if (command.equalsIgnoreCase("STAT")) { processSTAT(arguments); } else if (command.equalsIgnoreCase("LIST")) { processLIST(arguments); } else if (command.equalsIgnoreCase("RETR")) { processRETR(arguments); } else if (command.equalsIgnoreCase("DELE")) { processDELE(arguments); } else if (command.equalsIgnoreCase("NOOP")) { processTOP(arguments); } else if (command.equalsIgnoreCase("TOP")) { processTOP(arguments); } else if (command.equalsIgnoreCase("UIDL")) { processUIDL(arguments); } else if (command.equalsIgnoreCase("RSET")) { processRSET(arguments); } else if (command.equalsIgnoreCase("QUIT")) { processQUIT(arguments); } else {

out.println("-ERR Unknown command " + command); }

}

protected void processUIDL(StringTokenizer arguments) { if (!arguments.hasMoreTokens()) {

out.println("+OK " + countMessages() + " " + getMessagesSize()); for (int i = 1; i <= messages.size(); i++) {

Message message = getMessage(i); if (message != null) {

} }

out.println("."); } else {

String messageNumber = arguments.nextToken(); Message message = getMessage(messageNumber); if (message == null) {

out.println("-ERR no such message"); } else {

out.println("+OK " + messageNumber + " " + message.getMessageId()); }

} }

/**

* Nhaọn keỏt noỏi vaứ caực leọnh POP3 gửỷi lẽn tửứ trỡnh khaựch */

public void run() { try {

in = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

out = new PrintStream(socket.getOutputStream()); } catch (IOException e) { e.printStackTrace(System.err); close(); return; } if (server == null) {

System.err.println("SERVER NOT SET!!!"); return;

} (adsbygoogle = window.adsbygoogle || []).push({});

stopRequested = false; state = ENTER_USER;

out.println("+OK POP3 " + Server.getAddress()); while (!stopRequested) {

try {

if (line == null) break;

StringTokenizer tokenizer = new StringTokenizer(line); String command = "";

if (tokenizer.hasMoreTokens())

command = tokenizer.nextToken(); // Xửỷ lyự leọnh tuyứ theo tráng thaựi ủaờng nhaọp cuỷa user

switch (state) { case ENTER_USER: processEnterUser(command, tokenizer); break; case ENTER_PASSWORD: processEnterPassword(command, tokenizer); break; case TRANSACTION: processTransaction(command, tokenizer); break; default:

out.println("-ERR invalid state! "); System.err.println("Invalid State in POPConnection.run()");

stopRequested = true; break; } } catch (Exception e) { e.printStackTrace(System.err); stopRequested = true; } }

// ẹoựng keỏt noỏi close();

server.removeConnection(this); }

public void setServer(POPServer server) {} }

2.3.CAỉI ẹAậT MAILCLIENT

Phần MailClient ủửụùc thieỏt keỏ dửụựi dáng Web duứng Java Server Page nẽn ủửụùc gói laứ moọt WebMail. Trang Web vaứ quaự trỡnh sửỷ dúng mailclient coự theồ ủửụùc hỡnh dung qua sụ ủồ vaứ ủaởc taỷ ủửụựi ủãy

1.ẹaờng nhaọp heọ thoỏng :

Lần ủầu tiẽn sửỷ dúng CT ủeồ gửỷi nhaọn mail, chuựng ta phaỷi ủaờng kyự moọt account mụựi cho riẽng mỡnh, nghúa laứ chuựng ta phaỷi coự moọt username vaứ password

HomePage

ẹaờng Kyự Thaứnh Viẽn

Login Help

Menumail

Inbox AddressBook Option

Add Edit Review

Inf PasswordChange Hỡnh caỏu truực Website

riẽng cho mỡnh nhaốm phãn bieọt nhửừng ngửụứi sửỷ dúng email khaực nhau vaứ baỷo maọt thử cuỷa tửứng ngửụứi.

_ ẹaờng nhaọp username vaứ password trửụực khi vaứo heọ thoỏng . Neỏu laứ “member”: bán coự theồ sửỷ dúng CT gửỷi /nhaọn thử . Neỏu laứ “guest”: bán phaỷi ẹaờng kyự thaứnh viẽn

2.ẹaờng kyự thaứnh viẽn :

Cho lần ủầu tiẽn bán ủaờng nhaọp vaứo heọ thoỏng , ghi caực thõng tin caự nhãn, username vaứ password cuỷa bán.

3.Caực loái Address Book . ẹiều chổnh thõng tin trong Personal Address Book : (User coự quyền T, X, S addressbook cuỷa mỡnh)

Caực thõng tin về ủũa chổ mail cuỷa caực Client ủửụùc lửu trửừ trong caực soồ ủũa chổ. Moĩi ngửụứi sửỷ dúng mail thửụứng coự moọt soồ ủũa chổ caự nhãn (Personal Address Book - PAB) cuỷa riẽng mỡnh vaứ ủửụùc lửu trửừ trong oồ ủúa cúc boọ . (adsbygoogle = window.adsbygoogle || []).push({});

Caực thõng tin cần thieỏt khi thẽm moọt ủũa chổ E-mail vaứo PAB:

_ RealName laứ hó tẽn ủầy ủuỷ cuỷa ngửụứi coự ủũa chổ mail cần thẽm vaứo PAB _ E-mail Address ủũa chổ E-mail chớnh xaực cuỷa ngửụứi cần thẽm vaứo.

Ngoaứi ra, chuựng ta coự theồ thẽm vaứo nhửừng ủũa chổ email khaực, soỏ phone, mobile vaứ ủũa chổ nhaứ riẽng

4.Review Account Information:

_ User xem lai thõng tin caự nhãn ủaừ ủaờng nhaọp vaứ AddressBook cuỷa mỡnh. _ Coự theồ thay ủoồi Password (changepassword.asp) khi bán muoỏn ủaởt lái moọt password mụựi cho mỡnh.

5.Thay ủoồi Pass :

_ Old Password: nhaọp lái pass hieọn tái.

_ New password: nhaọp pass mụựi. Bán coự theồ sửỷ dúng caực kyự tửù chửừ, soỏ vaứ caỷ heọ thoỏng chaỏm cãu, nhửng khõng theồ sửỷ dúng khoaỷng traộng.

_ Nhaọp lái password moọt lần nửừa. Neỏu so truứng, vieọc thay ủoồi password xem nhử thaứnh cõng.

6.Kieồm tra thử:

Vuứng noọi dung caực Folder seừ xuaỏt hieọn tiẽu ủề caực thử trong folder ủoự. Caực thử chửa ủửụùc ủóc thửụứng ủửụùc trỡnh baứy khaực vụựi thử ủaừ ủửụùc ủóc. Thõng thửụứng phần tiẽu ủềthử gồm caực noọi dung nhử sau:

_ From: Tẽn ngửụứi gửỷi, noọi dung xuaỏt hieọn ụỷ ủãy khõng phaỷi laứ ủũa chổ e- mail cuỷa ngửụứi gửỷi maứ laứ hó tẽn cuỷa ngửụứi gửỷi.

_ Subject : Chuỷ ủề cuỷa laự thử . Khi gửỷi thử, ngửoứi gửỷi thửụứng ghi moọt noọi dung ngaộn vaứo vuứng subject ủeồ ngửụứi nhaọn bieỏt ủửụùc múc ủớch toồng quaựt cuỷa laự thử trửụực khi quyeỏt ủũnh coự ủóc thử hay khõng.

_ Date : Ngaứy giụứ nhaọn thử

_ Size: Kớch thửụực laự thử (thửụứng tớnh baống KB) 7.ẹóc thử :

Taỏt caỷ thử cuỷa bán ủửụùc lieọt kẽ trong moọt baỷng trong Inbox vụựi moĩi haứng laứ moọt thử. ẹeồ xem noọi dung thử cần click vaứo vuứng tiẽu ủề cuỷa thử. Trong vuứng noọi dung thử, caực thõng tin cuỷa ngửoứi gửỷi seừ ủửụùc theồ hieọn cuứng noọi dung laự thử . Caực thõng tin ngửụứi gửỷi bao gồm: tẽn ngửụứi gửỷi, thụứi ủieồm gửỷi, tẽn ngửụứi nhaọn

Muoỏn ủóc email naứo, click vaứo coọt From tửụng ửựng. Sau khi ủóc xong, ủeồ ủóc thử khaực, click vaứo Inbox hay click vaứo nuựt Back cuỷa Internet Explorer.

_ Sau khi ủóc xong thử ,Delete neỏu muoỏn xoựa thử. _ Reply neỏu muoỏn traỷ lụứi thử

_ Download attach file neỏu thử coự gửỷi file keứm theo . INBOX

CheckMail Upload File Compose

Read Reply Delete

DownLoad

Send Error

8.Vieỏt email trong WebMail: Chuựng ta coự hai trửụứng hụùp:

• Neỏu bán ủang ủóc moọt thử naứo ủoự, bán muoỏn traỷ lụứi thử ủoựthỡ click vaứo nuựt Reply phớa trẽn.

• Neỏu bán muoỏn gửỷi thử trửùc tieỏp, click vaứo nuựt Compose.

Khi muoỏn gửỷi thử cho moọt ngửụứi ủaừ coự ủũa chổ trong moọt AddressBook, ủầu tiẽn phaỷi chuaồn bũ noọi dung thử ủeồ gửỷi. Phần tiẽu ủề cuỷa moọt thử cần gửỷi bao gồm caực thõng tin sau:

_ To : ẹũa chổ email maứ bán muoỏn gửỷi tụựi.

_ Subject: moọt ủoán text ngaộn cho bieỏt múc ủớch cuỷa noọi dung thử cần gửỷi. ẹoán text naứy giuựp cho ngửụứi ủóc thử quyeỏt ủũnh coự nẽn xem noọi dung cuỷa thử hay khõng.

_ Attachments gụỷi keứm taọp tin cho nguửụứi nhaọn. (adsbygoogle = window.adsbygoogle || []).push({});

_ Noọi dung soán trong comment

_ Sau khi soán thaỷo noọi dung thử xong, nhaỏn nuựt Send ủeồ thử ủửụùc caỏt vaứo Outbox. Nghúa laứ, thử khõng ủửụùc truyền ủi ngay, do ủoự chuựng ta coự theồ soán thaỷo nhiều thử trửụực khi keỏt noỏi vụựi Mail Server ủeồ gửỷi ủi.

Khi thử ủaừ ủửa vaứo Outbox, mói chổnh sửỷa tieỏp theo thửụứng laứm thử khõng gửỷi ủi ủửụùc nửừa. Trửụứng hụùp naứy cần phaỷi soán thaỷo moọt laự thử khaực ủeồ gửỷi ủi vaứ xoựa boỷ laự thử cuừ.

9.Gửỷi keứm file theo Mail (attach file)

Trong moọt soỏ trửụứng hụùp cần ủửa thẽm toaứn boọ noọi dung moọt taọp tin ủang coự saỹn trẽn ủúa cúc boọ vaứo thử ủeồ gửỷi ủeỏn ngửoứi nhaọn. Cõng vieọc naứy ủửụùc gói laứ Attach file vaứo thử.

Cần chuự yự raống vieọc attach moọt taọp tin vaứo mail cần phaỷi ủửụùc thửùc hieọn ủồng boọ giửừa ngửụứi gửỷi , ngửụứi nhaọn vaứ ủửụứng truyền thử.Neỏu khõng coự sửù ủồng boọ, noọi dung cuỷa taọp tin ủửụùc attach coự ngửụứi nhaọn khõng ủóc ủửụùc. Lyự do cuỷa vieọc naứy laứ do noọi dung cuỷa taọp tin ủửụùc attach seừ ủửụùc maừ hoaự theo UUENCODE hay theo MIME (Multipurpose Internet Mail Extensions).

Thõng thửụứng ủeồ giaỷm thụứi gian truyền thử, caực taọp tin cần attach vaứo thử seừ ủửụùc neựn trửụực khi thửùc hieọn thao taực attach. Caựch neựn thõng dúng laứ ZIP.

Caực taọp tin ủửụùc gửỷi keứm coự theồ lửu thaứnh moọt taọp tin riẽng trong ủiaừ cúc boọ cuỷa ngửụứi sửỷ dúng. Thõng thửụứng dung lửụùng taọp tin gửỷi keứm khõng nẽn quaự 1MB, neỏu lụựn hụn ta cần taựch ra gửỷi noự trong nhiều thử.

10. Download / Upload file

Trang ủaờng nhaọp cuỷa vietmail

Menu chớnh

• Phãn tớch, thieỏt keỏ vaứ caứi ủaởt heọ thoỏng thõng tin quaỷn lyự - Hieọu ủớnh: GS. Bách Hửng Khang

• Cụ sụỷ dửừ lieọu quan heọ - Lẽ Taỏn Vửụng

• System Administrator for Microsoft SQL Serever 7.0 workbook

• Microsoft SQL Server 7.0 Database Implementation Training Kit

• Microsoft SQL Server hoách ủũnh vaứ xãy dửùng cụ sụỷ dửừ lieọu cao caỏp

• Allaire JRUN Developer Documentation

• Enterprise JavaBeans – Tom Valesky

• Enterprise JavaBeans by Example – Henri Jubin, Jurgen Friendichs

• HTML by Example – Todd Stauffer

• Special Edition Using HTML 4 – Jerry Honeycutt

• Special Edition Using JavaScript – Mark C. Reynolds

• Java by Example – Clayton Walnum

• Laọp trỡnh Java theỏ naứo? – Hoaứng Ngóc Giao

• Java laọp trỡnh máng – Nguyeĩn Phửụng Lan, Hoaứng ẹửực Haỷi

Một phần của tài liệu Giao thức truyền thông trên Internet (Trang 73 - 93)