001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.mail; 025 026 import java.io.Closeable; 027 import java.security.Security; 028 import java.util.Properties; 029 030 import javax.mail.Folder; 031 import javax.mail.MessagingException; 032 import javax.mail.Session; 033 import javax.mail.Store; 034 035 import org.ujmp.core.listmatrix.DefaultListMatrix; 036 import org.ujmp.core.listmatrix.ListMatrix; 037 038 public class ImapUtil implements Closeable { 039 040 private String host = null; 041 042 private String user = null; 043 044 private String password = null; 045 046 private final int connectiontimeout = 5000; 047 048 private final int timeout = 5000; 049 050 private final String protocol = "imaps"; 051 052 private final boolean sslEnable = true; 053 054 private final int port = 993; 055 056 private final boolean ignoreCertificate = true; 057 058 private Session session = null; 059 060 private Store store = null; 061 062 public ImapUtil(String host, String user, String password) { 063 this.host = host; 064 this.user = user; 065 this.password = password; 066 Properties props = System.getProperties(); 067 props.setProperty("mail.imap.host", host); 068 props.setProperty("mail.imap.port", "" + port); 069 props 070 .setProperty("mail.imap.connectiontimeout", "" 071 + connectiontimeout); 072 props.setProperty("mail.imap.timeout", "" + timeout); 073 props.setProperty("mail.store.protocol", protocol); 074 props.setProperty("mail.imap.ssl.enable", "" + sslEnable); 075 076 if (ignoreCertificate) { 077 Security.setProperty("ssl.SocketFactory.provider", 078 DummySSLSocketFactory.class.getName()); 079 } 080 081 session = Session.getDefaultInstance(System.getProperties(), null); 082 } 083 084 public ListMatrix<Folder> getPersonalFolders() throws Exception { 085 Folder[] folders = getStore().getPersonalNamespaces(); 086 ListMatrix<Folder> folderMatrix = new DefaultListMatrix<Folder>(folders); 087 return folderMatrix; 088 } 089 090 public ListMatrix<Folder> getSubFolders(Folder folder) throws Exception { 091 Folder[] folders = folder.list(); 092 ListMatrix<Folder> folderMatrix = new DefaultListMatrix<Folder>(folders); 093 return folderMatrix; 094 } 095 096 public MessagesMatrix getMessages(String folderName) throws Exception { 097 Folder folder = getStore().getFolder(folderName); 098 return getMessages(folder); 099 } 100 101 public Folder getFolder(String folderName) throws Exception { 102 Folder folder = getStore().getFolder(folderName); 103 return folder; 104 } 105 106 public ListMatrix<Folder> getSubFolders(String folderName) throws Exception { 107 Folder folder = getStore().getFolder(folderName); 108 return getSubFolders(folder); 109 } 110 111 public MessagesMatrix getMessages(Folder folder) throws Exception { 112 return new MessagesMatrix(folder); 113 } 114 115 private Store getStore() throws Exception { 116 if (store == null) { 117 store = session.getStore(protocol); 118 } 119 if (!store.isConnected()) { 120 store.connect(host, user, password); 121 } 122 return store; 123 } 124 125 public void close() { 126 if (store != null && store.isConnected()) { 127 try { 128 store.close(); 129 } catch (MessagingException e) { 130 e.printStackTrace(); 131 } 132 } 133 } 134 135 public ListMatrix<Folder> getSharedFolders() throws Exception { 136 Folder[] folders = getStore().getSharedNamespaces(); 137 ListMatrix<Folder> folderMatrix = new DefaultListMatrix<Folder>(folders); 138 store.close(); 139 return folderMatrix; 140 } 141 142 }