Simple POP3 Class


/ Published in: Java
Save to your folder(s)

This code snippet presents a simple POP3 class that can be used to get emails.


Copy this code and paste it in your HTML
  1. import java.net.Socket;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStreamWriter;
  4. import java.io.BufferedReader;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7.  
  8. class Program {
  9.  
  10. public static void main(String[] args){
  11. POP3 popClient = new POP3("pop.host.com", 110);
  12. popClient.setCredentials("username", "password");
  13.  
  14. System.out.println("Message count: " + popClient.getMessageCount());
  15. System.out.println("First message:");
  16.  
  17. POP3Message message = popClient.getMessage(1);
  18.  
  19. System.out.println("From: " + message.getFrom());
  20. System.out.println("To: " + message.getTo());
  21. System.out.println("Subject: " + message.getSubject());
  22. System.out.println("Date: " + message.getDate());
  23. System.out.println("Message: " + message.getBody());
  24. }
  25. }
  26.  
  27. class POP3{
  28.  
  29. protected Socket socket;
  30. protected BufferedReader reader;
  31. protected OutputStreamWriter writer;
  32. protected ArrayList<String> log = new ArrayList<String>();
  33.  
  34. public POP3(String host, int port){
  35. try{
  36. socket = new Socket(host, port);
  37. reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  38. writer = new OutputStreamWriter(socket.getOutputStream());
  39.  
  40. log.add(reader.readLine());
  41. }catch(Exception e){
  42. log.add(e.getMessage());
  43. }
  44. }
  45.  
  46. public void setCredentials(String username, String password){
  47. try{
  48. writer.write("USER " + username + "\n");
  49. writer.flush();
  50. log.add(reader.readLine());
  51. writer.write("PASS " + password + "\n");
  52. writer.flush();
  53.  
  54. log.add(reader.readLine());
  55. }catch(Exception ioe){
  56. log.add(ioe.getMessage());
  57. }
  58. }
  59.  
  60. public POP3Message getMessage(int msgIndex){
  61.  
  62. POP3Message message = new POP3Message();
  63.  
  64. try{
  65. writer.write("RETR " + Integer.toString(msgIndex) + "\n");
  66. writer.flush();
  67.  
  68. HashMap<String, String> headerMap = new HashMap<String, String>();
  69. boolean headers = true;
  70. String response = "";
  71. String body = "";
  72.  
  73. while(true){
  74. response = reader.readLine();
  75.  
  76. if (response.trim().equals(".")){
  77. break;
  78. }
  79.  
  80. if(response.trim().length() !=0 && headers){
  81. String[] parts = response.split(":", 2);
  82. try{
  83. headerMap.put(parts[0].trim(), parts[1].trim());
  84. }catch(Exception e){
  85. }
  86. }else{
  87. headers = false;
  88. }
  89.  
  90. if(headers == false){
  91. body +=response;
  92. }
  93. }
  94.  
  95. message.setFrom(headerMap.get("From"));
  96. message.setTo(headerMap.get("To"));
  97. message.setSubject(headerMap.get("Subject"));
  98. message.setDate(headerMap.get("Date"));
  99. message.setBody(body);
  100.  
  101. }catch(Exception ioe){
  102. log.add(ioe.getMessage());
  103. }
  104. return message;
  105. }
  106.  
  107. public int getMessageCount(){
  108. try{
  109. writer.write("STAT\n");
  110. writer.flush();
  111. String[] parts = reader.readLine().split(" ");
  112. return Integer.parseInt(parts[1]);
  113. }catch(Exception ioe){
  114. log.add(ioe.getMessage());
  115. }
  116. return 0;
  117. }
  118.  
  119. public ArrayList<String> getLog(){
  120. return this.log;
  121. }
  122. }
  123.  
  124. class POP3Message{
  125.  
  126. protected String from = "";
  127. protected String to = "";
  128. protected String subject = "";
  129. protected String date = "";
  130. protected String body = "";
  131.  
  132. public void setFrom(String from){
  133. this.from = from;
  134. }
  135.  
  136. public String getFrom(){
  137. return this.from;
  138. }
  139.  
  140. public void setTo(String to){
  141. this.to = to;
  142. }
  143.  
  144. public String getTo(){
  145. return this.to;
  146. }
  147.  
  148. public void setSubject(String subject){
  149. this.subject = subject;
  150. }
  151.  
  152. public String getSubject(){
  153. return this.subject;
  154. }
  155.  
  156. public void setDate(String date){
  157. this.date = date;
  158. }
  159.  
  160. public String getDate(){
  161. return this.date;
  162. }
  163.  
  164. public void setBody(String body){
  165. this.body = body;
  166. }
  167.  
  168. public String getBody(){
  169. return this.body;
  170. }
  171. }

URL: http://www.hostprojects.net/snippets/java/145/simple-pop3-class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.