/ Published in: Java
This code snippet presents a simple POP3 class that can be used to get emails.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 { POP3 popClient = new POP3("pop.host.com", 110); popClient.setCredentials("username", "password"); POP3Message message = popClient.getMessage(1); } } class POP3{ protected ArrayList<String> log = new ArrayList<String>(); try{ log.add(reader.readLine()); log.add(e.getMessage()); } } try{ writer.write("USER " + username + "\n"); writer.flush(); log.add(reader.readLine()); writer.write("PASS " + password + "\n"); writer.flush(); log.add(reader.readLine()); log.add(ioe.getMessage()); } } public POP3Message getMessage(int msgIndex){ POP3Message message = new POP3Message(); try{ writer.flush(); boolean headers = true; while(true){ response = reader.readLine(); if (response.trim().equals(".")){ break; } if(response.trim().length() !=0 && headers){ try{ headerMap.put(parts[0].trim(), parts[1].trim()); } }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); log.add(ioe.getMessage()); } return message; } public int getMessageCount(){ try{ writer.write("STAT\n"); writer.flush(); log.add(ioe.getMessage()); } return 0; } public ArrayList<String> getLog(){ return this.log; } } class POP3Message{ this.from = from; } return this.from; } this.to = to; } return this.to; } this.subject = subject; } return this.subject; } this.date = date; } return this.date; } this.body = body; } return this.body; } }
URL: http://www.hostprojects.net/snippets/java/145/simple-pop3-class