1 22 23 package gnu.mail.providers.imap4; 24 25 import java.io.IOException ; 26 import java.net.UnknownHostException ; 27 import javax.mail.Folder ; 28 import javax.mail.MessagingException ; 29 import javax.mail.Session ; 30 import javax.mail.Store ; 31 import javax.mail.URLName ; 32 33 import javax.mail.Flags ; 34 35 41 public class IMAPStore 42 extends Store 43 { 44 45 public static void main(String [] args) { 47 try { 48 Session session = Session.getDefaultInstance(System.getProperties()); 49 session.setDebug(true); 50 URLName url = new URLName ("imap://localhost/"); 51 Store s = session.getStore(url); 52 s.connect("localhost", "test", "imaptest"); 53 Folder f = s.getFolder("INBOX"); 54 f.open(Folder.READ_ONLY); 55 javax.mail.Message [] m = f.getMessages(); 56 for (int i=0; i<m.length; i++) { 57 System.out.println(m[i].getSubject()); 58 Flags flags = m[i].getFlags(); 61 if (flags.contains(Flags.Flag.FLAGGED)) 62 flags.remove(Flags.Flag.FLAGGED); 63 else 64 flags.add(Flags.Flag.FLAGGED); 65 m[i].setFlags(flags, true); 66 } 67 f.close(false); 68 s.close(); 69 } catch (Exception e) { 70 e.printStackTrace(System.err); 71 } 72 } 73 75 78 public static final int DEFAULT_PORT = 143; 79 80 83 protected IMAPConnection connection; 84 85 88 protected IMAPFolder root; 89 90 93 public IMAPStore(Session session, URLName url) 94 { 95 super(session, url); 96 } 97 98 102 protected boolean protocolConnect(String host, int port, String username, 103 String password) 104 throws MessagingException 105 { 106 if (port<0) port = DEFAULT_PORT; 107 if (host==null || username==null || password==null) 108 return false; 109 if (connection!=null) 110 return true; 111 synchronized (this) 112 { 113 try 114 { 115 connection = new IMAPConnection(host, port); 116 if (session.getDebug()) 117 connection.setDebug(true); 118 return connection.login(username, password); 119 } 120 catch (UnknownHostException e) 121 { 122 throw new MessagingException (e.getMessage(), e); 123 } 124 catch (IOException e) 125 { 126 throw new MessagingException (e.getMessage(), e); 127 } 128 } 129 } 130 131 134 public synchronized void close() 135 throws MessagingException 136 { 137 if (connection!=null) 138 { 139 synchronized (this) 140 { 141 try 142 { 143 connection.logout(); 144 } 145 catch (IOException e) 146 { 147 } 148 connection = null; 149 } 150 } 151 super.close(); 152 } 153 154 157 public Folder getDefaultFolder() 158 throws MessagingException 159 { 160 return getFolder(""); 161 } 162 163 166 public Folder getFolder(String name) 167 throws MessagingException 168 { 169 return new IMAPFolder(this, name); 170 } 171 172 175 public Folder getFolder(URLName urlname) 176 throws MessagingException 177 { 178 return getFolder(urlname.getFile()); 179 } 180 181 } 182 | Popular Tags |