Overview
This article will demonstrate how to communicate with a POP3 server using Secure iNet Factory.
Code Example
Download POPEx.java
01 import com.jscape.inet.email.EmailMessage;
02 import com.jscape.inet.pop.*;
03 import java.util.*;
04
05 public class POPEx extends PopAdapter
06 {
07 Pop pop = null;
08
09 public POPEx(String server,
10 String username,
11 String password,
12 boolean setApopAuthMode) throws PopException
13 {
14 // create new Pop instance
15 pop = new Pop(server, username, password);
16 // set authentication mode to use APOP
17 if (setApopAuthMode) pop.setAuthMode(Pop.AUTH_APOP);
18 // mark messages retrieved for deletion upon logout
19 pop.setDelete(true);
20 }
21
22 public void openConnection() throws PopException
23 {
24 pop.connect();
25 }
26
27 public void closeConnection() throws PopException
28 {
29 pop.disconnect();
30 }
31
32 public void connected(PopConnectedEvent event)
33 {
34 System.out.println("Connected to host: " + event.getHostname());
35 }
36
37 public void disconnected(PopDisconnectedEvent event)
38 {
39 System.out.println("Disconnected from host: " + event.getHostname());
40 }
41
42 public void commandSent(PopCommandEvent event)
43 {
44 System.out.println("Command : " + event.getCommand());
45 }
46
47 public void responseReceived(PopResponseEvent event)
48 {
49 System.out.println("Response : " + event.getResponse());
50 }
51
52 public int getMessageCount() throws PopException
53 {
54 return pop.getMessageCount();
55 }
56
57 public Enumeration getMessages() throws PopException
58 {
59 return pop.getMessages();
60 }
61
62 public EmailMessage getMessage(int id) throws PopException
63 {
64 return pop.getMessage(id);
65 }
66
67 public void deleteMessage(int id) throws PopException
68 {
69 pop.deleteMessage(id);
70 }
71
72 public String issueCommand(String cmd) throws PopException
73 {
74 return pop.issueCommand("TOP 1 11");
75 }
76
77 public static void main(String[] args)
78 {
79 try
80 {
81 POPEx pe = new POPEx("pop.myserver.com","jsmith","secret", true);
82 pe.openConnection();
83 int count = pe.getMessageCount();
84 // each message is of type EmailMessage
85 Enumeration messages = pe.getMessages();
86 // get message with a particular id
87 EmailMessage message = pe.getMessage(0);
88 // delete message with a particular id
89 pe.deleteMessage(0);
90 pe.closeConnection();
91 }
92 catch (Exception e)
93 {
94 e.printStackTrace();
95 }
96 }
97 }
Lines 1-3 : Necessary import statements. Lines 9-20 : Create a new Pop instance. Lines 22-30 : Methods for opening and closing connection to the POP3 server. Lines 27-50 : Event handlers from PopAdapter. Lines 52-55 : Get message count. Lines 57-60 : Get messages from the server. Lines 62-65 : Get message by id. Lines 67-70 : Delete message by id. Lines 72-75 : Issue a command to the remote server.
Go here for a detailed article on POP protocol.
|
Comments