1 36 package QuickIMAP; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.nio.charset.Charset ; 40 import java.text.DateFormat ; 41 42 import org.columba.ristretto.coder.Base64DecoderInputStream; 43 import org.columba.ristretto.coder.CharsetDecoderInputStream; 44 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream; 45 import org.columba.ristretto.imap.IMAPException; 46 import org.columba.ristretto.imap.IMAPHeader; 47 import org.columba.ristretto.imap.IMAPProtocol; 48 import org.columba.ristretto.imap.SearchKey; 49 import org.columba.ristretto.imap.SequenceSet; 50 import org.columba.ristretto.io.StreamUtils; 51 import org.columba.ristretto.log.RistrettoLogger; 52 import org.columba.ristretto.message.BasicHeader; 53 import org.columba.ristretto.message.MailboxInfo; 54 import org.columba.ristretto.message.MimeHeader; 55 import org.columba.ristretto.message.MimePart; 56 import org.columba.ristretto.message.MimeTree; 57 58 public class QuickIMAP { 59 60 private static final String [] HEADER = new String [] {"From","Date" , "Subject"}; 61 62 private static final String helpMessage = 63 "Usage : QuickIMAP imap-server username password\n\n" 64 + "Example: QuickIMAP imap.mail.com myname mypassword\n\n"; 65 66 67 70 public static void main(String [] args) { 71 if( args.length < 3 ) { 72 System.out.println(helpMessage); 73 return; 74 } 75 String server = args[0]; 76 String username = args[1]; 77 String password = args[2]; 78 79 81 IMAPProtocol protocol = new IMAPProtocol(server, IMAPProtocol.DEFAULT_PORT); 82 try { 83 System.out.println("Connecting to " + server + " ..."); 84 protocol.openPort(); 85 86 System.out.println("Login in as "+ username + " ..."); 87 protocol.login(username, password.toCharArray()); 88 89 System.out.print("Checking Inbox: "); 90 MailboxInfo info = protocol.select("INBOX"); 91 if( info.getFirstUnseen() != -1 ) { 92 Integer [] unseenMails = protocol.search(new SearchKey[] { new SearchKey(SearchKey.UNSEEN) }); 93 94 System.out.println(unseenMails.length + " unseen message(s)."); 95 96 IMAPHeader[] header = protocol.fetchHeaderFields(new SequenceSet(unseenMails), HEADER ); 97 98 for( int i=header.length-1; i >= 0; i--) { 99 System.out.println( "==="); 100 printHeader(header[i]); 101 102 MimeTree mimeTree = protocol.fetchBodystructure(unseenMails[i].intValue()); 103 MimePart textPart = mimeTree.getFirstTextPart("plain"); 104 105 if( textPart != null ) { 106 InputStream body = protocol.fetchBody(unseenMails[i].intValue(), textPart.getAddress()); 107 MimeHeader textHeader = textPart.getHeader(); 108 109 if( textHeader.getContentTransferEncoding() == MimeHeader.QUOTED_PRINTABLE) { 110 body = new QuotedPrintableDecoderInputStream(body); 111 } else if( textHeader.getContentTransferEncoding() == MimeHeader.BASE64) { 112 body = new Base64DecoderInputStream(body); 113 } 114 115 String charsetName = textHeader.getContentParameter("charset"); 116 if( charsetName == null ) { 117 charsetName = System.getProperty("file.encoding"); 118 } 119 120 body = new CharsetDecoderInputStream(body, Charset.forName(charsetName)); 121 122 System.out.println("---"); 123 System.out.println(StreamUtils.readInString(body)); 124 System.out.println("---"); 125 } 126 127 if( mimeTree.count() > 1 || textPart == null) { 128 System.out.println("message has attachments"); 129 System.out.println("---"); 130 } 131 132 } 133 } else { 134 System.out.println(" no unseen messages found."); 135 } 136 137 protocol.logout(); 138 139 } catch (IOException e) { 140 System.err.println("IO Error: " + e.getLocalizedMessage()); 141 142 } catch (IMAPException e) { 143 System.err.println("IMAP Error: " + e.getMessage()); 144 145 try { 146 protocol.logout(); 147 } catch (IOException e1) { 148 } catch (IMAPException e1) { 149 } 150 } 151 152 System.exit(0); 153 } 154 155 private static void printHeader(IMAPHeader rawHeader) { 156 BasicHeader header = new BasicHeader(rawHeader.getHeader()); 157 158 System.out.println(header.getFrom().toString()); 159 System.out.println(DateFormat.getInstance().format(header.getDate())); 160 System.out.println(header.getSubject()); 161 } 162 163 164 } 165 | Popular Tags |