KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > QuickIMAP


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package QuickIMAP;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.nio.charset.Charset JavaDoc;
40 import java.text.DateFormat JavaDoc;
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 JavaDoc[] HEADER = new String JavaDoc[] {"From","Date" , "Subject"};
61
62     private static final String JavaDoc helpMessage =
63         "Usage : QuickIMAP imap-server username password\n\n"
64             + "Example: QuickIMAP imap.mail.com myname mypassword\n\n";
65     
66     
67     /**
68      * @param args
69      */

70     public static void main(String JavaDoc[] args) {
71         if( args.length < 3 ) {
72             System.out.println(helpMessage);
73             return;
74         }
75         String JavaDoc server = args[0];
76         String JavaDoc username = args[1];
77         String JavaDoc password = args[2];
78
79         //RistrettoLogger.setLogStream(System.out);
80

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 JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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