Revision: 68961
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 26, 2015 23:06 by syedhussim
Initial Code
import java.net.Socket; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.util.ArrayList; import java.util.HashMap; class Program { public static void main(String[] args){ POP3 popClient = new POP3("pop.host.com", 110); popClient.setCredentials("username", "password"); System.out.println("Message count: " + popClient.getMessageCount()); System.out.println("First message:"); POP3Message message = popClient.getMessage(1); System.out.println("From: " + message.getFrom()); System.out.println("To: " + message.getTo()); System.out.println("Subject: " + message.getSubject()); System.out.println("Date: " + message.getDate()); System.out.println("Message: " + message.getBody()); } } class POP3{ protected Socket socket; protected BufferedReader reader; protected OutputStreamWriter writer; protected ArrayList<String> log = new ArrayList<String>(); public POP3(String host, int port){ try{ socket = new Socket(host, port); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new OutputStreamWriter(socket.getOutputStream()); log.add(reader.readLine()); }catch(Exception e){ log.add(e.getMessage()); } } public void setCredentials(String username, String password){ try{ writer.write("USER " + username + "\n"); writer.flush(); log.add(reader.readLine()); writer.write("PASS " + password + "\n"); writer.flush(); log.add(reader.readLine()); }catch(Exception ioe){ log.add(ioe.getMessage()); } } public POP3Message getMessage(int msgIndex){ POP3Message message = new POP3Message(); try{ writer.write("RETR " + Integer.toString(msgIndex) + "\n"); writer.flush(); HashMap<String, String> headerMap = new HashMap<String, String>(); boolean headers = true; String response = ""; String body = ""; while(true){ response = reader.readLine(); if (response.trim().equals(".")){ break; } if(response.trim().length() !=0 && headers){ String[] parts = response.split(":", 2); try{ headerMap.put(parts[0].trim(), parts[1].trim()); }catch(Exception e){ } }else{ headers = false; } if(headers == false){ body +=response; } } message.setFrom(headerMap.get("From")); message.setTo(headerMap.get("To")); message.setSubject(headerMap.get("Subject")); message.setDate(headerMap.get("Date")); message.setBody(body); }catch(Exception ioe){ log.add(ioe.getMessage()); } return message; } public int getMessageCount(){ try{ writer.write("STAT\n"); writer.flush(); String[] parts = reader.readLine().split(" "); return Integer.parseInt(parts[1]); }catch(Exception ioe){ log.add(ioe.getMessage()); } return 0; } public ArrayList<String> getLog(){ return this.log; } } class POP3Message{ protected String from = ""; protected String to = ""; protected String subject = ""; protected String date = ""; protected String body = ""; public void setFrom(String from){ this.from = from; } public String getFrom(){ return this.from; } public void setTo(String to){ this.to = to; } public String getTo(){ return this.to; } public void setSubject(String subject){ this.subject = subject; } public String getSubject(){ return this.subject; } public void setDate(String date){ this.date = date; } public String getDate(){ return this.date; } public void setBody(String body){ this.body = body; } public String getBody(){ return this.body; } }
Initial URL
http://www.hostprojects.net/snippets/java/145/simple-pop3-class
Initial Description
This code snippet presents a simple POP3 class that can be used to get emails.
Initial Title
Simple POP3 Class
Initial Tags
Initial Language
Java