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 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
1 |
public class FetchMailProImpl implements FetchMailPro { |
36 |
|
|
37 |
1 |
private static Log log = LogFactory.getLog(FetchMailProImpl.class); |
38 |
|
|
39 |
|
|
40 |
|
public static final String DEFAULT_HOST = "localhost"; |
41 |
|
|
42 |
|
|
43 |
|
public static final String DEFAULT_PROTOCOL = "pop3"; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public static final int DEFAULT_PORT = -1; |
50 |
|
|
51 |
|
private static final String INBOX_NAME = "INBOX"; |
52 |
|
|
53 |
1 |
private String host = DEFAULT_HOST; |
54 |
|
|
55 |
1 |
private String protocol = DEFAULT_PROTOCOL; |
56 |
|
|
57 |
1 |
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 |
1 |
public FetchMailProImpl() { |
73 |
1 |
System.setProperty("mail.mime.multipart.ignoremissingendboundary", "true"); |
74 |
1 |
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public synchronized void connect() throws MailException { |
80 |
0 |
log.debug(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤¹¡£"); |
81 |
|
|
82 |
0 |
Session session = Session.getInstance(createProperties(), null); |
83 |
0 |
if (javaMailLogEnabled) { |
84 |
0 |
session.setDebug(true); |
85 |
|
} |
86 |
|
|
87 |
|
try { |
88 |
0 |
store = session.getStore(protocol); |
89 |
0 |
} catch (NoSuchProviderException e) { |
90 |
0 |
log.error("»ØÄꤵ¤?¤¿¥×¥úÁÈ¥³¥?[" + protocol + "]¤Ï¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Þ¤»¤ó¡£", e); |
91 |
0 |
throw new MailException("»ØÄꤵ¤?¤¿¥×¥úÁÈ¥³¥?[" + protocol + "]¤Ï¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Þ¤»¤ó¡£", e); |
92 |
|
} |
93 |
|
|
94 |
|
try { |
95 |
0 |
store.connect(host, port, username, password); |
96 |
0 |
} catch (AuthenticationFailedException e) { |
97 |
0 |
log.error(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳǧ¾Ú¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
98 |
0 |
throw new MailAuthenticationException(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host |
99 |
0 |
+ "]¤Ø¤ÎÀܳǧ¾Ú¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
100 |
0 |
} catch (MessagingException e) { |
101 |
0 |
log.error(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
102 |
0 |
throw new MailException(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
103 |
|
} |
104 |
|
|
105 |
0 |
log.info(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤·¤¿¡£"); |
106 |
|
|
107 |
0 |
changeFolder(INBOX_NAME); |
108 |
0 |
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
private Properties createProperties() { |
117 |
0 |
Properties prop = new Properties(); |
118 |
0 |
if ("apop".equalsIgnoreCase(protocol)) { |
119 |
0 |
prop.put("mail.pop3.apop.enable", "true"); |
120 |
|
} |
121 |
0 |
return prop; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
public synchronized void disconnect() throws MailException { |
128 |
|
try { |
129 |
0 |
closeCurrentFolderIfOpen(); |
130 |
0 |
} finally { |
131 |
0 |
if (isConnected()) { |
132 |
0 |
log.debug(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£"); |
133 |
|
try { |
134 |
0 |
store.close(); |
135 |
0 |
store = null; |
136 |
0 |
} catch (MessagingException e) { |
137 |
0 |
throw new MailException("¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
138 |
|
} |
139 |
|
} |
140 |
0 |
} |
141 |
0 |
log.info(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤·¤¿¡£"); |
142 |
0 |
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
private void closeCurrentFolderIfOpen() throws MailException { |
150 |
0 |
if (currentFolder != null && currentFolder.isOpen()) { |
151 |
0 |
log.debug("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + currentFolder.getName() + "]¤ò¥¯¥ú½¼¥º¤·¤Þ¤¹¡£"); |
152 |
|
try { |
153 |
0 |
currentFolder.close(true); |
154 |
0 |
} catch (MessagingException e) { |
155 |
0 |
log.error("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + currentFolder.getName() + "]¤Î¥¯¥ú½¼¥º¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
156 |
0 |
throw new MailException("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + currentFolder.getName() + "]¤Î¥¯¥ú½¼¥º¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", |
157 |
0 |
e); |
158 |
|
} |
159 |
0 |
log.debug("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + currentFolder.getName() + "]¤ò¥¯¥ú½¼¥º¤·¤Þ¤·¤¿¡£"); |
160 |
0 |
currentFolder = null; |
161 |
|
} |
162 |
0 |
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
public synchronized void changeFolder(String folderName) throws MailException { |
168 |
0 |
closeCurrentFolderIfOpen(); |
169 |
0 |
log.debug("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + folderName + "]¤ò¥ª¡¼¥×¥ó¤·¤Þ¤¹¡£"); |
170 |
|
try { |
171 |
0 |
currentFolder = store.getFolder(folderName); |
172 |
0 |
currentFolder.open(Folder.READ_WRITE); |
173 |
0 |
} catch (MessagingException e) { |
174 |
0 |
log.error("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + folderName + "]¤Î¥ª¡¼¥×¥ó¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
175 |
0 |
throw new MailException("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + folderName + "]¤Î¥ª¡¼¥×¥ó¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
176 |
|
} |
177 |
0 |
log.debug("¥á¥Ã¥»¡¼¥¸¥Õ¥©¥?¥À[" + folderName + "]¤ò¥ª¡¼¥×¥ó¤·¤Þ¤·¤¿¡£"); |
178 |
0 |
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
public int getMailCount() throws MailException { |
184 |
0 |
checkIfCurrentFolderIsOpen(); |
185 |
|
try { |
186 |
0 |
return currentFolder.getMessageCount(); |
187 |
0 |
} catch (MessagingException e) { |
188 |
0 |
throw new MailFetchException("¥á¡¼¥?¿ô¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
private void checkIfCurrentFolderIsOpen() throws NotConnectedException { |
199 |
0 |
if (currentFolder == null || !currentFolder.isOpen()) { |
200 |
0 |
throw new NotConnectedException(protocol.toUpperCase() + "¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤µ¤?¤Æ¤¤¤Þ¤»¤ó¡£"); |
201 |
|
} |
202 |
0 |
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
public ReceivedMail getMail(int num) throws MailException { |
208 |
0 |
MimeMessage mimeMessage = getMessage(num); |
209 |
0 |
MailConverter converter = new MailConverter(mimeMessage); |
210 |
0 |
return converter.convertIntoMails()[0]; |
211 |
|
} |
212 |
|
|
213 |
|
public ReceivedMail[] getMails(boolean delete) throws MailException { |
214 |
0 |
MimeMessage[] mimeMessages = getMessages(delete); |
215 |
0 |
MailConverter converter = new MailConverter(mimeMessages); |
216 |
0 |
return converter.convertIntoMails(); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
public MimeMessage getMessage(int num) throws MailException { |
223 |
0 |
checkIfCurrentFolderIsOpen(); |
224 |
|
try { |
225 |
0 |
return (MimeMessage)currentFolder.getMessage(num); |
226 |
0 |
} catch (MessagingException e) { |
227 |
0 |
log.error("¥á¥Ã¥»¡¼¥¸¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
228 |
0 |
throw new MailFetchException("¥á¥Ã¥»¡¼¥¸¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
public MimeMessage[] getMessages(boolean delete) throws MailException { |
233 |
0 |
checkIfCurrentFolderIsOpen(); |
234 |
|
try { |
235 |
0 |
Message[] messages = currentFolder.getMessages(); |
236 |
0 |
if (log.isInfoEnabled()) { |
237 |
0 |
if (messages.length > 0) { |
238 |
0 |
log.info(messages.length + "Ä̤Υ᡼¥?¤ò¼õ¿®¤·¤Þ¤¹¡£"); |
239 |
|
} else { |
240 |
0 |
log.info("¼õ¿®¤¹¤?¥á¡¼¥?¤Ï¤¢¤ê¤Þ¤»¤ó¡£"); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
0 |
currentFolder.setFlags(messages, new Flags(Flags.Flag.SEEN), true); |
245 |
|
|
246 |
0 |
if (delete) { |
247 |
0 |
currentFolder.setFlags(messages, new Flags(Flags.Flag.DELETED), true); |
248 |
|
} |
249 |
0 |
MimeMessage[] mimeMessages = new MimeMessage[messages.length]; |
250 |
0 |
for (int i = 0; i < messages.length; i++) { |
251 |
0 |
mimeMessages[i] = (MimeMessage)messages[i]; |
252 |
|
} |
253 |
0 |
return mimeMessages; |
254 |
0 |
} catch (MessagingException e) { |
255 |
0 |
log.error("¥á¥Ã¥»¡¼¥¸¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
256 |
0 |
throw new MailFetchException("¥á¥Ã¥»¡¼¥¸¤Î¼èÆÀ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
public boolean isConnected() { |
264 |
0 |
return store != null && store.isConnected(); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
public String getHost() { |
273 |
0 |
return host; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
public void setHost(String host) { |
283 |
1 |
this.host = host; |
284 |
1 |
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
public String getPassword() { |
292 |
0 |
return password; |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
public void setPassword(String password) { |
301 |
1 |
this.password = password; |
302 |
1 |
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
public String getProtocol() { |
310 |
0 |
return protocol; |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
public void setProtocol(String protocol) { |
324 |
1 |
this.protocol = protocol; |
325 |
1 |
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
public String getUsername() { |
331 |
0 |
return username; |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
|
|
339 |
|
public void setUsername(String username) { |
340 |
1 |
this.username = username; |
341 |
1 |
} |
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
public int getPort() { |
347 |
0 |
return port; |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
public void setPort(int port) { |
357 |
0 |
this.port = port; |
358 |
0 |
} |
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
public boolean isJavaMailLogEnabled() { |
366 |
0 |
return javaMailLogEnabled; |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
public void setJavaMailLogEnabled(boolean javaMailLogEnabled) { |
378 |
0 |
this.javaMailLogEnabled = javaMailLogEnabled; |
379 |
0 |
} |
380 |
|
} |