1 package com.ozacc.mail.fetch.impl;
2
3 import java.util.Properties;
4
5 import javax.mail.AuthenticationFailedException;
6 import javax.mail.Flags;
7 import javax.mail.Folder;
8 import javax.mail.Message;
9 import javax.mail.MessagingException;
10 import javax.mail.NoSuchProviderException;
11 import javax.mail.Session;
12 import javax.mail.Store;
13 import javax.mail.internet.MimeMessage;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import com.ozacc.mail.MailAuthenticationException;
19 import com.ozacc.mail.MailException;
20 import com.ozacc.mail.NotConnectedException;
21 import com.ozacc.mail.fetch.FetchMailPro;
22 import com.ozacc.mail.fetch.MailFetchException;
23 import com.ozacc.mail.fetch.ReceivedMail;
24
25 /***
26 * <code>FetchMail</code>インターフェースの実装クラス。
27 * <p>
28 * このクラスのインスタンスは、インスタンス変数を用いて状態を保持するため、
29 * ステートレスではありません。ステートフルです。
30 *
31 * @since 1.2
32 * @author Tomohiro Otsuka
33 * @version $Id: FetchMailProImpl.java,v 1.1.2.8 2005/01/19 14:23:06 otsuka Exp $
34 */
35 public class FetchMailProImpl implements FetchMailPro {
36
37 private static Log log = LogFactory.getLog(FetchMailProImpl.class);
38
39 /*** デフォルトのSMTPサーバ。「localhost」 */
40 public static final String DEFAULT_HOST = "localhost";
41
42 /*** デフォルトのプロトコル。「pop3」 */
43 public static final String DEFAULT_PROTOCOL = "pop3";
44
45 /***
46 * デフォルトのポート。「-1」<br>
47 * -1はプロトコルに応じた適切なポートを設定する特別な値。
48 */
49 public static final int DEFAULT_PORT = -1;
50
51 private static final String INBOX_NAME = "INBOX";
52
53 private String host = DEFAULT_HOST;
54
55 private String protocol = DEFAULT_PROTOCOL;
56
57 private int port = DEFAULT_PORT;
58
59 private String username;
60
61 private String password;
62
63 private boolean javaMailLogEnabled;
64
65 private Store store;
66
67 private Folder currentFolder;
68
69 /***
70 * コンストラクタ。
71 */
72 public FetchMailProImpl() {
73 System.setProperty("mail.mime.multipart.ignoremissingendboundary", "true");
74 }
75
76 /***
77 * @see com.ozacc.mail.fetch.FetchMailPro#connect()
78 */
79 public synchronized void connect() throws MailException {
80 log.debug(protocol.toUpperCase() + "サーバ[" + host + "]に接続します。");
81
82 Session session = Session.getInstance(createProperties(), null);
83 if (javaMailLogEnabled) {
84 session.setDebug(true);
85 }
86
87 try {
88 store = session.getStore(protocol);
89 } catch (NoSuchProviderException e) {
90 log.error("指定されたプロトコル[" + protocol + "]はサポートされていません。", e);
91 throw new MailException("指定されたプロトコル[" + protocol + "]はサポートされていません。", e);
92 }
93
94 try {
95 store.connect(host, port, username, password);
96 } catch (AuthenticationFailedException e) {
97 log.error(protocol.toUpperCase() + "サーバ[" + host + "]への接続認証に失敗しました。", e);
98 throw new MailAuthenticationException(protocol.toUpperCase() + "サーバ[" + host
99 + "]への接続認証に失敗しました。", e);
100 } catch (MessagingException e) {
101 log.error(protocol.toUpperCase() + "サーバ[" + host + "]への接続に失敗しました。", e);
102 throw new MailException(protocol.toUpperCase() + "サーバ[" + host + "]への接続に失敗しました。", e);
103 }
104
105 log.info(protocol.toUpperCase() + "サーバ[" + host + "]に接続しました。");
106
107 changeFolder(INBOX_NAME);
108 }
109
110 /***
111 * Sessionに渡すPropertiesインスタンスを返します。
112 * APOP認証を行う場合に、"mail.pop3.apop.enable"をセットします。
113 *
114 * @return Sessionに渡すPropertiesインスタンス
115 */
116 private Properties createProperties() {
117 Properties prop = new Properties();
118 if ("apop".equalsIgnoreCase(protocol)) {
119 prop.put("mail.pop3.apop.enable", "true");
120 }
121 return prop;
122 }
123
124 /***
125 * @see com.ozacc.mail.fetch.FetchMailPro#disconnect()
126 */
127 public synchronized void disconnect() throws MailException {
128 closeCurrentFolderIfOpen();
129 log.debug(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断します。");
130 try {
131 store.close();
132 store = null;
133 } catch (MessagingException e) {
134 throw new MailException("サーバ[" + host + "]との接続切断に失敗しました。", e);
135 }
136 log.info(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断しました。");
137 }
138
139 /***
140 * 現在のメッセージフォルダをクローズします。
141 *
142 * @throws MailException メッセージフォルダのクローズに失敗した場合
143 */
144 private void closeCurrentFolderIfOpen() throws MailException {
145 if (currentFolder != null && currentFolder.isOpen()) {
146 log.debug("メッセージフォルダ[" + currentFolder.getName() + "]をクローズします。");
147 try {
148 currentFolder.close(true);
149 } catch (MessagingException e) {
150 log.error("メッセージフォルダ[" + currentFolder.getName() + "]のクローズに失敗しました。", e);
151 throw new MailException("メッセージフォルダ[" + currentFolder.getName() + "]のクローズに失敗しました。",
152 e);
153 }
154 log.debug("メッセージフォルダ[" + currentFolder.getName() + "]をクローズしました。");
155 currentFolder = null;
156 }
157 }
158
159 /***
160 * @see com.ozacc.mail.fetch.FetchMailPro#changeFolder(java.lang.String)
161 */
162 public synchronized void changeFolder(String folderName) throws MailException {
163 closeCurrentFolderIfOpen();
164 log.debug("メッセージフォルダ[" + folderName + "]をオープンします。");
165 try {
166 currentFolder = store.getFolder(folderName);
167 currentFolder.open(Folder.READ_WRITE);
168 } catch (MessagingException e) {
169 log.error("メッセージフォルダ[" + folderName + "]のオープンに失敗しました。", e);
170 throw new MailException("メッセージフォルダ[" + folderName + "]のオープンに失敗しました。", e);
171 }
172 log.debug("メッセージフォルダ[" + folderName + "]をオープンしました。");
173 }
174
175 /***
176 * @see com.ozacc.mail.fetch.FetchMailPro#getMailCount()
177 */
178 public int getMailCount() throws MailException {
179 checkIfCurrentFolderIsOpen();
180 try {
181 return currentFolder.getMessageCount();
182 } catch (MessagingException e) {
183 throw new MailFetchException("メール数の取得に失敗しました。", e);
184 }
185 }
186
187 /***
188 * メールサーバに接続されていて、フォルダが操作できる状態かどうか調べます。
189 * フォルダが操作できる状態にない場合、NotConnectedExceptionをスローします。
190 *
191 * @throws NotConnectedException
192 */
193 private void checkIfCurrentFolderIsOpen() throws NotConnectedException {
194 if (currentFolder == null || !currentFolder.isOpen()) {
195 throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続されていません。");
196 }
197 }
198
199 /***
200 * @see com.ozacc.mail.fetch.FetchMailPro#getMail(int)
201 */
202 public ReceivedMail getMail(int num) throws MailException {
203 MimeMessage mimeMessage = getMessage(num);
204 MailConverter converter = new MailConverter(mimeMessage);
205 return converter.convertIntoMails()[0];
206 }
207
208 public ReceivedMail[] getMails(boolean delete) throws MailException {
209 MimeMessage[] mimeMessages = getMessages(delete);
210 MailConverter converter = new MailConverter(mimeMessages);
211 return converter.convertIntoMails();
212 }
213
214 /***
215 * @see com.ozacc.mail.fetch.FetchMailPro#getMessage(int)
216 */
217 public MimeMessage getMessage(int num) throws MailException {
218 checkIfCurrentFolderIsOpen();
219 try {
220 return (MimeMessage)currentFolder.getMessage(num);
221 } catch (MessagingException e) {
222 log.error("メッセージの取得に失敗しました。", e);
223 throw new MailFetchException("メッセージの取得に失敗しました。", e);
224 }
225 }
226
227 public MimeMessage[] getMessages(boolean delete) throws MailException {
228 checkIfCurrentFolderIsOpen();
229 try {
230 Message[] messages = currentFolder.getMessages();
231 if (log.isInfoEnabled()) {
232 if (messages.length > 0) {
233 log.info(messages.length + "通のメールを受信します。");
234 } else {
235 log.info("受信するメールはありません。");
236 }
237 }
238
239 currentFolder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
240
241 if (delete) {
242 currentFolder.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
243 }
244 MimeMessage[] mimeMessages = new MimeMessage[messages.length];
245 for (int i = 0; i < messages.length; i++) {
246 mimeMessages[i] = (MimeMessage)messages[i];
247 }
248 return mimeMessages;
249 } catch (MessagingException e) {
250 log.error("メッセージの取得に失敗しました。", e);
251 throw new MailFetchException("メッセージの取得に失敗しました。", e);
252 }
253 }
254
255 /***
256 * @see com.ozacc.mail.fetch.FetchMailPro#isConnected()
257 */
258 public boolean isConnected() {
259 return store.isConnected();
260 }
261
262 /***
263 * メールサーバのホスト名、またはIPアドレスを返します。
264 *
265 * @return メールサーバのホスト名、またはIPアドレス
266 */
267 public String getHost() {
268 return host;
269 }
270
271 /***
272 * メールサーバのホスト名、またはIPアドレスをセットします。
273 * デフォルトは localhost です。
274 *
275 * @param host メールサーバのホスト名、またはIPアドレス
276 */
277 public void setHost(String host) {
278 this.host = host;
279 }
280
281 /***
282 * メールサーバの認証パスワードを返します。
283 *
284 * @return メールサーバの認証パスワード
285 */
286 public String getPassword() {
287 return password;
288 }
289
290 /***
291 * メールサーバの認証パスワード名をセットします。
292 *
293 * @param password メールサーバの認証パスワード
294 */
295 public void setPassword(String password) {
296 this.password = password;
297 }
298
299 /***
300 * メール受信に使用するプロトコロルをセットします。
301 *
302 * @return プロトコル
303 */
304 public String getProtocol() {
305 return protocol;
306 }
307
308 /***
309 * メール受信に使用するプロトコロルをセットします。
310 * 現在サポートされているプロトコルは、「pop3」と「imap」の二つです。
311 * デフォルトは「pop3」です。
312 * <p>
313 * POP3サーバへの認証をAPOPで行いたい場合は、プロトコル名ではありませんが、
314 * 「apop」を指定してください。APOP認証を使用するには、JavaMail 1.3.2以降が必要です。
315 *
316 * @param protocol プロトコル
317 */
318 public void setProtocol(String protocol) {
319 this.protocol = protocol;
320 }
321
322 /***
323 * @return 認証ユーザ名
324 */
325 public String getUsername() {
326 return username;
327 }
328
329 /***
330 * メールサーバの認証ユーザ名をセットします。
331 *
332 * @param username 認証ユーザ名
333 */
334 public void setUsername(String username) {
335 this.username = username;
336 }
337
338 /***
339 * @return ポート番号
340 */
341 public int getPort() {
342 return port;
343 }
344
345 /***
346 * メール受信に使用するポート番号をセットします。
347 * プロトコルに応じたポート番号が自動的に使用されますので、通常ここでポート番号をセットする必要はありません。
348 *
349 * @param port ポート番号
350 */
351 public void setPort(int port) {
352 this.port = port;
353 }
354
355 /***
356 * JavaMailのデバッグが有効かどうか判定します。
357 *
358 * @return JavaMailのデバッグが有効な場合 ture
359 */
360 public boolean isJavaMailLogEnabled() {
361 return javaMailLogEnabled;
362 }
363
364 /***
365 * JavaMailのデバッグを有効にするかどうか指定します。
366 * 有効にすると、<code>System.out</code>のデバッグメッセージが出力されます。<br>
367 * デフォルトは無効になっています。
368 *
369 * @see javax.mail.session#setDebug(boolean)
370 * @param javaMailLogEnabled The javaMailLogEnabled to set.
371 */
372 public void setJavaMailLogEnabled(boolean javaMailLogEnabled) {
373 this.javaMailLogEnabled = javaMailLogEnabled;
374 }
375 }