Overview
This article will demonstrate how to use the imap component in Secure iNet Factory to communicate with an IMAP server in Java. Go here to view a detailed article.
Code Example
Download IMAPEx.java
001 import com.jscape.inet.imap.Imap;
002 import com.jscape.inet.imap.ImapAdapter;
003 import com.jscape.inet.imap.ImapConnectedEvent;
004 import com.jscape.inet.imap.ImapDisconnectedEvent;
005 import com.jscape.inet.imap.ImapException;
006 import com.jscape.inet.imap.ImapMessage;
007 import com.jscape.inet.email.*;
008 import java.util.*;
009
010 public class IMAPEx extends ImapAdapter
011 {
012 private Imap imap = null;
013
014 public IMAPEx(String server,
015 String username,
016 String password,
017 int timeout,
018 int auth)
019 {
020 this.imap = new Imap(server, username, password);
021 // set authentication mode.
022 imap.setAuthMode(auth);
023 // set connection timeout to 5 seconds
024 // argument to setConnectTimeout is in milliseconds.
025 imap.setConnectTimeout(timeout);
026
027 // add listener so this instance can receive events from
028 // ImapAdapter
029 imap.addImapListener(this);
030 }
031
032 public void connect() throws ImapException
033 {
034 imap.connect();
035 }
036
037 public String issueCommand(String command) throws ImapException
038 {
039 return imap.issueCommand(command);
040 }
041
042 public void releaseConnection(boolean expunge) throws ImapException
043 {
044 if (expunge) imap.expunge();
045 imap.disconnect();
046 }
047
048 public int getMessageCount() throws ImapException
049 {
050 return imap.getMessageCount();
051 }
052
053 public Vector getNewMessages() throws ImapException
054 {
055 Vector ret = new Vector();
056 Enumeration e = imap.getNewMessages();
057 while(e.hasMoreElements())
058 ret.add(e.nextElement());
059 return ret;
060 }
061
062 public Vector getAllMessages() throws ImapException
063 {
064 Vector ret = new Vector();
065 Enumeration e = imap.getMessages();
066 while(e.hasMoreElements())
067 ret.add(e.nextElement());
068 return ret;
069 }
070
071 public EmailMessage getEmailMessage(int index) throws ImapException
072 {
073 return (EmailMessage)imap.getMessage(index);
074 }
075
076 public Vector getAnsweredMessages() throws ImapException
077 {
078 Vector ret = new Vector();
079 Enumeration e = imap.getMessagesWithFlags();
080 while(e.hasMoreElements())
081 {
082 ImapMessage m = (ImapMessage)e.nextElement();
083 if (m.isAnswered() == true) ret.add(m.getMessage());
084 }
085 return ret;
086 }
087
088 public Vector getDrafts() throws ImapException
089 {
090 Vector ret = new Vector();
091 Enumeration e = imap.getMessagesWithFlags();
092 while(e.hasMoreElements())
093 {
094 ImapMessage m = (ImapMessage)e.nextElement();
095 if (m.isDraft() == true) ret.add(m.getMessage());
096 }
097 return ret;
098 }
099
100 public void markMessagesUponDeletion(boolean value)
101 {
102 imap.setDelete(value);
103 }
104
105 public void deleteMessage(int index) throws ImapException
106 {
107 imap.deleteMessage(index);
108 }
109
110 public Vector getMessagesWithMinimumSize(int size) throws ImapException
111 {
112 Vector ret = new Vector();
113 Integer i = new Integer(size);
114 Enumeration e = imap.getMessages("LARGER " + i.toString());
115 while(e.hasMoreElements())
116 ret.add(e.nextElement());
117 return ret;
118 }
119
120 public void store(int start, int end, int mode, int flag) throws ImapException
121 {
122 imap.store(start, end, mode, flag);
123 }
124
125 public void connected(ImapConnectedEvent event)
126 {
127 System.out.println("Connected to host: " + event.getHostname());
128 }
129
130 public void disconnected(ImapDisconnectedEvent event)
131 {
132 System.out.println("Disconnected from host: " + event.getHostname());
133 }
134
135 public static void main(String[] args)
136 {
137 try
138 {
139 // create an IMAPEx instance object.
140 IMAPEx imap = new IMAPEx("imap.myserver.com","jsmith","secret",
141 5000, Imap.AUTH_CRAM_MD5);
142
143 // connect and login
144 imap.connect();
145 // send EXPUNGE command to server
146 String response = imap.issueCommand("EXPUNGE");
147 // get the number of messages in the mailbox.
148 int messageCount = imap.getMessageCount();
149 // get new messages from the server
150 Vector newMessages = imap.getNewMessages();
151 // get all messages from the server
152 Vector allMessages = imap.getAllMessages();
153 // retrieve the first message in the mailbox
154 EmailMessage message = imap.getEmailMessage(0);
155 // get answered messages.
156 Vector messages = imap.getAnsweredMessages();
157 // get drafts
158 messages = imap.getDrafts();
159 imap.markMessagesUponDeletion(true);
160 imap.deleteMessage(0);
161 // close the connection
162 // get messages with a minumum size
163 messages = imap.getMessagesWithMinimumSize(10000);
164 // Sets message flags for a range of messages in an IMAP mailbox.
165 // add \Answered flag to messages 1 thru 10 to existing flags
166 imap.store(1,10,Imap.FLAG_MODE_ADD,Imap.FLAG_ANSWERED);
167
168 // close the connection
169 // argument is a boolean value specifying to expunge
170 // delete messages from the server.
171 imap.releaseConnection(true);
172 }
173 catch (Exception e)
174 {
175 e.printStackTrace();
176 }
177
178 }
179 }
Lines 1-8 : Necessary import statements. Line 20 : Initialize imap instance with the given hostname, username and password. Line 22 : Set authentication mode. Line 25 : Set connection timeout. Line 29 : Add listener for receiving events. Lines 32-35 : Connect to the remote server. Lines 37-40 : Issue a command to the remote server. Lines 42-46 : Close connection to the remote server. Lines 48-51 : Get the number of messages Lines 53-60 : Get all new messages from the mail server. Lines 62-69 : Get all messages from the mail server. Lines 71-74 : Get a message at a particular index. Lines 76-86 : Get all answered messages from the server. Lines 88-98 : Get all drafts from the server. Lines 100-103 : Set a flag to expunge deleted messages from the server. Lines 105-108 : Delete a message at a particular index. Lines 110-118 : Get messages with a minimum size. Lines 120-123 : Sets message flags for a range of messages in an IMAP mailbox. Lines 125-133 : Events from the listener.
Before using this code replace hostname, username and password passed to the IMAPEx constructor with your own credentials. |
Comments