Pop3 Server Dengan Socket Programming Dalam Bahasa Java | Email | Online Services

POP3 Server dengan Socket Programming dalam Bahasa Java

POP3 Server dengan Socket Programming dalam Bahasa Java
Jeffrey Hermanto Halimsetiawan shadowz_029@yahoo.com.sg tutorialpemrograman.wordpress.com 3 Juni 2009

tutorialpemrograman.wordpress.com - 2009

1

cukup setting salah satu Mail Client tersebut dengan email address misalnya : jeffrey@localhost.com> Date: Tue.com .2009 2 . e-mail yang terdapat dalam POP3 Server dapat di-retreive dengan menggunakan telnet ataupun Mail Client seperti Outlook Express. tutorialpemrograman. tinggal lakukan receive all maka semua e-mail untuk user jeffrey akan masuk ke inbox. nantinya aka nada file jeffrey dan jeff.76. Nyoba PJK jeh.wordpress. nantinya akan terdapat file user. Nah..com (Postfix) with SMTP id 5E5DBEAFC for <yoga>. Post Office Protocol (POP) digunakan untuk membaca email sedangkan Simple Mail Transfer Protocol (SMTP) digunakan untuk mengirim email. Untuk mengakses mailbox tersebut dibutuhkan otentikasi akses dengan menggunakan konfigurasi tertentu seperti username dan password.203]) by klp47. dan lain sebagainya. Mailbox tersebut memiliki quota space yang diset oleh administrator email server tersebut. Jadi. 7 Apr 2009 23:08:03 +0700 (WIT) Message-Id: <20090407160823.com Received: from hai (unknown [10. mungkin masih ada yang bingung mengenai perbedaan POP dan SMTP.. atau yang biasa disebut mail account. 7 Apr 2009 23:08:03 +0700 (WIT) From: amel@klp47. Nantinya.com To: undisclosed-recipients:. Dalam program POP3 Server yang akan dibuat ini.. Jika program POP3 Server nya telah di-run .POP3 Server dengan Socket Programming dalam Bahasa Java POP3 Server adalah tempat dimana email ditampung dari berbagai pengirim yang dibagi dalam mailboxmailbox dimana masing masing mailbox memiliki alamat yang unik dari pemiliknya.com Tue Apr 7 23:08:41 2009 Return-Path: <amel@klp47. contoh : jeffrey jeff 12345 12345 Kemudian setiap user akan mempunyai file yang bernama seperti username nya yang didalamnya berisi e-mail yang formatnya seperti format di atas yang ditujukan untuk user tersebut. dalam tulisan ini akan dijelaskan bagaimana caranya untuk membuat POP3 Server sederhana dengan memanfaatkan pemrograman socket dalam Bahasa Java. Tue. POP3 Server : localhost.5E5DBEAFC@klp47..151. Sebuah mail account memiliki konfigurasi yang berisi informasi teknis untuk dapat masuk dan membaca email email yang berada di POP3 Server. Mozilla Thunderbird. Sebenarnya format e-mail yang dibaca oleh e-mail client adalah seperti ini : From amel@klp47. dan POP3 port : 1804.txt yang menyimpan username dan password . Kemudian . Selain itu.com> X-Original-To: jeffrey Delivered-To: jeffrey@klp47.

nextToken()). } } catch (Exception ex) { } } //method untuk me-load data-data message yang ada public static void loadMessage(ListMessage listMessage. if (temp.io.addUser(st. while(true) { PopRequest request = new PopRequest(listenSocket.FileReader. ServerSocket listenSocket = new ServerSocket(PORT).readLine(). listUser.matches("") ){ listMessage. st.wordpress. int PORT = 1804. /** * * @author Jeffrey */ public class Main { public static ListUser listUser = new ListUser().io. */ package jhhs. BufferedReader br = new BufferedReader(fr). jhhs. BufferedReader br = new BufferedReader(fr). tempMsg = .readLine()). String tempMsg = "". while (br.BufferedReader.nextToken().2009 tutorialpemrograman.java /* * To change this template.ListUser. Thread thread = new Thread(request).ListMessage. while (br.POP3 Server dengan Socket Programming dalam Bahasa Java Main.matches("")){ 3 . java. } if (!tempMsg.txt").unit. } tempMsg += temp +"\n".start().com "". choose Tools | Templates * and open the template in the editor. String user){ try { FileReader fr = new FileReader(user). thread.StringTokenizer. java.contains("From " ) && !tempMsg. jhhs.util.ready()){ StringTokenizer st = new StringTokenizer(br.unit.addMessage(tempMsg). } } //method untuk me-load data-data user yang terdaftar private static void loadUser(ListUser listUser){ try { FileReader fr = new FileReader("user. public static void main(String [] args) throws Exception{ loadUser(listUser). import import import import import import java.net.accept()).ServerSocket. java.ready()){ String temp = br.

com . if (temp. public PopRequest(Socket client) throws Exception { this. } if (!tempMsg. this. this.listMsg = new ListMessage().login = false. java.2009 this.Socket.io.net.util.addMessage(tempMsg). private boolean login. choose Tools | Templates * and open the template in the editor. private boolean stop.matches("") ){ listMessage.ListMessage.java /* * To change this template. tempMsg = "".processRequest().client = client.DataInputStream. this.unit. public ListMessage listMsg. java. } } catch (Exception ex) { } } } PopRequest. */ package jhhs.matches("")){ listMessage.DataOutputStream.stop = false.wordpress.User.Message. java. } tempMsg += temp +"\n".io. jhhs. jhhs.addMessage(tempMsg). java.printStackTrace(). import import import import import import import import java.POP3 Server dengan Socket Programming dalam Bahasa Java String temp = br. jhhs. public User curUser. this.StringTokenizer.readLine().welcome = false.InputStream. } } catch (Exception ex) { ex.io. /** * * @author Jeffrey */ public class PopRequest implements Runnable{ final static String CRLF ="\r\n".contains("From " ) && !tempMsg. private boolean welcome. public Socket client. 4 .unit.unit. } public void run() { try { while (!stop){ tutorialpemrograman.

// close connection dos.stop = true. } String requestLine = "". if (!this. } if (this. br.close().user)){ dos."+CRLF). client.out. // close connection dos.writeBytes("+OK name is a valid mailbox. if (Main. //jika user melakukan ^] atau CTRL+] pada Telnet if (requestLine == null){ this. } .printStackTrace().findUser(this.print(requestLine).curUser. } System.equalsIgnoreCase("quit")){ this. client.client.read(ab).listUser. this.welcome){ dos.close().curUser = null.processRequest().stop = true.curUser. } } private void processRequest() throws Exception{ InputStream is = this.getInputStream(). this. if (! login){ if (command.curUser = new tutorialpemrograman.curUser == null){ if (command.2009 User(). requestLine += (char) counter.user = st. } } catch (Exception ex) { ex.close(). DataInputStream br = new DataInputStream(is). is. String command = st.wordpress.client.nextToken().writeBytes("+OK dewey POP3 server signing off.com . for (byte counter : ab){ if (counter == 0) break.equalsIgnoreCase("user") && st. is. dos. } StringTokenizer st = new StringTokenizer(requestLine).close(). 5 this."+CRLF).curUser = null.welcome = true. DataOutputStream dos = new DataOutputStream(this. byte ab[] = new byte[512].writeBytes("+OK Hello There. return."+CRLF).POP3 Server dengan Socket Programming dalam Bahasa Java public void run() { try { while (!stop){ this.nextToken().close().getOutputStream()). this.hasMoreTokens()){ this.close().

com .writeBytes(msg).curUser = new User(). boolean authenticated = Main.writeBytes("+OK name is a valid mailbox.writeBytes("-ERR command not found!!"+CRLF). String msg = "". if (authenticated){ this.writeBytes(".hasMoreTokens()){ this.equalsIgnoreCase("list")){ if (!st.size())+" messages in maildrop.hasMoreTokens()){ this. Message temp = this. for (Message iterator : this. } } else { dos.POP3 Server dengan Socket Programming dalam Bahasa Java if (this.curUser. dos. this."+CRLF). no mailbox for "+this.listMsg.size())+" messages. i++.curUser = null. } } else { //command untuk pass if (command. dos. tutorialpemrograman. int i = 1.equalsIgnoreCase("pass") && st."+CRLF)."+CRLF). this.writeBytes("-ERR invalid password.curUser = null.listMsg. if (Main.nextToken().curUser.parseInt(st.list){ msg += String.length()) + CRLF.list.user+" here."+CRLF). } dos.text.listMsg. } } } else { // Command untuk list if (command.wordpress.curUser.user. this.null){ if (temp == 2009 dos.user = st."+CRLF).listUser.valueOf(i) + " " + Integer.curUser.writeBytes("+OK maildrop locked and ready. Main.loadMessage(this. this.valueOf(this.pass = st.writeBytes("-ERR no such message.curUser.pass).writeBytes("+OK "+String. only "+String.equalsIgnoreCase("user") && st. this.listMsg.writeBytes("-ERR command not found!!"+CRLF).curUser.authenticate(this.curUser.login = true.listMsg.nextToken()."+CRLF).curUser == null){ if (command.listUser.list.valueOf(this. } } else { dos.toOctalString(iterator. } else { dos."+CRLF). } else { int pil = Integer.user).findUser(this.nextToken()).writeBytes("-ERR sorry.user)){ dos. msg = "".findMessage(pil). } else { 6 .hasMoreTokens()){ dos. } else{ dos.

} else { if (!temp. int length = this."+CRLF).listMsg.writeBytes("+OK "+count+" "+length+".writeBytes(". if (temp == null){ dos.nextToken()). } else if (command.parseInt(st.2009 this.nextToken()).writeBytes("+OK message "+String. } else dos. } else { dos.equalsIgnoreCase("retr")){ if (st.writeBytes("+OK "+Integer. this.toOctalString(temp. } else if (command.POP3 Server dengan Socket Programming dalam Bahasa Java else { int pil = Integer. Message temp = this.text+CRLF).writeBytes("-ERR no such message.nextToken()). } } } else dos."+CRLF).countMessage(). this."+CRLF).valueOf(this.listMsg.writeBytes("-ERR no such message.stop = true.listMsg. Message temp = this. dos.curUser."+CRLF)."+CRLF).valueOf(choice)+" deleted."+CRLF).valueOf(pil)+" "+Integer. if (temp == null){ dos.curUser = null.list. int count = this.valueOf(choice)+" already deleted.listMsg.println(choice).deleted = true. } else if (command. dos.equalsIgnoreCase("stat")){ int count = this."+CRLF).length())+" octets.user). Message temp = this."+CRLF).toOctalString(temp. //System.deleted){ temp. //System.writeBytes("-ERR message "+String.hasMoreTokens()){ int choice = Integer.hasMoreTokens()){ int choice = Integer. } dos.findMessage(pil).parseInt(st.wordpress.text. dos."+CRLF).println(choice).printToFile(this.writeBytes("-ERR no such message.countLengthMessage().deleteMessage(). } else { dos. if (temp == null){ dos.listMsg.writeBytes(temp.writeBytes("-ERR no such message.com .findMessage(choice). } } } // Command untuk Retr else if (command.equalsIgnoreCase("quit")){ tutorialpemrograman.listMsg. } else { dos.length())+CRLF).findMessage(choice).out.equalsIgnoreCase("dele")){ if (st.writeBytes(String."+CRLF).out.listMsg.listMsg.size())+" messages in maildrop.writeBytes("-ERR no such message.parseInt(st. only "+String. 7 .text.

writeBytes("-ERR command not found!!"+CRLF)."+CRLF). dos.writeBytes("+OK dewey POP3 server signing off (maildrop empty).user = user. public String pass.com . /** * * @author Jeffrey */ public class User { public String user.printToFile(this.close().POP3 Server dengan Socket Programming dalam Bahasa Java dos.stop = true.close(). this.user).close().2009 8 .wordpress. // close connection dos.curUser = null.listMsg. int count = this.writeBytes("+OK "+count+" "+length+".listMsg. public User() { } public User(String user. } else if (command. this.curUser. client.deleteMessage(). */ package jhhs. is. this.java /* * To change this template.unit. choose Tools | Templates * and open the template in the editor.pass = pass. } } } } User. } } tutorialpemrograman.equalsIgnoreCase("quit")){ this. String pass) { this. } else { dos."+CRLF).

} public boolean findUser(String user){ for (User iterator : this.unit.matches(user)){ return true. } } return false.ArrayList. /** * * @author Jeffrey */ public class ListUser { public ArrayList<User> list. public ListUser() { this.pass).list.matches(pass)){ return true. String pass){ User newUser = new User(user.out.com . } public boolean authenticate(String user.2009 9 .matches(user) && iterator.POP3 Server dengan Socket Programming dalam Bahasa Java ListUser.pass. } } } tutorialpemrograman.util.user.user. pass).list){ if (iterator.add(newUser). } public void showUser(){ for (User iterator : this.wordpress. import java.println(iterator.java package jhhs. this.list){ System.list){ if (iterator. String pass){ for (User iterator : this. } public void addUser(String user.list = new ArrayList<User>().user+" "+iterator. } } return false.

*/ package jhhs.POP3 Server dengan Socket Programming dalam Bahasa Java Message.add(newMessage). import java. this. public boolean deleted. public Message() { } public Message(String text) { this.ArrayList. */ package jhhs. tutorialpemrograman.text = text.java /* * To change this template.unit. /** * * @author Jeffrey */ public class ListMessage { public ArrayList<Message> list. import java. for (Message iterator : this. choose Tools | Templates * and open the template in the editor.FileWriter.util. public ListMessage() { this. import java.unit.list){ if (i == choice){ return iterator. 10 .list = new ArrayList<Message>(). } return null. } public void addMessage(String text){ Message newMessage = new Message(text).io.java /* * To change this template. choose Tools | Templates * and open the template in the editor. } public Message findMessage(int choice){ int i = 1.io. this.wordpress. } } ListMessage.deleted = false.2009 } i++.com . /** * * @author Jeffrey */ public class Message { public String text.list.BufferedWriter.

out. for (Message iterator : this.text).list){ if (!iterator.out.remove(counter. index = 0.intValue() .2009 return count.out.list.substring(0. System.out.com .println("Message : ").minus). } } tutorialpemrograman. } return null.text). } } public int countMessage(){ int count = 0. } int minus = 0.valueOf(iterator. } public void showMessage(){ for (Message iterator : this. ArrayList<Integer> del = new ArrayList<Integer>().deleted){ count += Integer.intValue() .minus).println(iterator.valueOf(index)). } public int deleteMessage(){ int count = 0.deleted){ System. for (Message iterator : this.list){ if (iterator.println(this.parseInt(String. for (Message iterator : this. count++. } return count. } public int countLengthMessage(){ int count = 0.list){ System. 30)).POP3 Server dengan Socket Programming dalam Bahasa Java public Message findMessage(int choice){ int i = 1.get(counter. } public void printToFile(String user){ FileWriter fw. } i++.minus). minus++.println(counter.length()).list){ if (!iterator. } } return count. this.text. } index++.8).println(iterator. System.list.out. del.deleted){ count++. for (Integer counter : del){ System.wordpress. 11 .add(Integer.list){ if (i == choice){ return iterator. for (Message iterator : this.text.intValue() .

parseInt(String. } bw.close().valueOf(iterator.flush(). fw.com .POP3 Server dengan Socket Programming dalam Bahasa Java Integer. bw. BufferedWriter bw = new BufferedWriter(fw).wordpress.2009 12 . try { fw = new FileWriter(user).list){ bw. for (Message iterator : this. } public void printToFile(String user){ FileWriter fw.8).write(iterator.close(). } } return count.text. } catch (Exception ex) { } } } tutorialpemrograman.length()).text).

Sign up to vote on this title
UsefulNot useful