001
002 package com.rain;
003
004 import java.io.*;
005 import java.util.*;
006 import javax.mail.*;
007 import javax.activation.*;
008 import javax.mail.internet.*;
009 import javax.servlet.http.*;
010
011 /**
012 * ¸ÞÀÏ °ü¸®±â (SMTP °ü·Ã)
013 * ¾ÆÁ÷ ¹Ì¿Ï¼ºº»ÀÓ (ÂüÁ¶¿ëÀ¸·Î¸¸ »ç¿ëÇÒ°Í)
014 * @version 1.0
015 * @author http://cafen.net (outmind@cafen.net)
016 */
017 public class rainMail {
018
019 /**
020 * ¸¶Áö¸· ¿À·ù ¸Þ¼¼Áö
021 */
022 public String error_msg = "";
023
024 /**
025 * ¸ÞÀÏ ÇüÅÂ
026 */
027 public String content_type = "text/html";
028
029 /**
030 * ¸ÞÀÏ ¾ð¾î¼Â
031 */
032 public String content_charset = "EUC-KR";
033
034 /**
035 * ¸ÞÀÏ Ã·ºÎÆÄÀÏ
036 */
037 public Vector content_attaches = new Vector();
038
039 /**
040 * ¸ÞÀÏ ½ºÅ侯
041 */
042 public Store store = null;
043
044 /**
045 * ¸ÞÀÏ Æúµå
046 */
047 public Folder[] folders = null;
048
049 /**
050 * Æúµå ÇöÀçÀ§Ä¡
051 */
052 public int folders_pos = -1;
053
054 /**
055 * ÇöÀç ¿¸° Æúµå
056 */
057 public Folder open_folder = null;
058
059 /**
060 * ÇöÀç ¿¸° ¸Þ¼¼Áö
061 */
062 public Message[] messages = null;
063
064 /**
065 * ÇöÀç ¿¸° ¸Þ¼¼ÁöÀÇ À§Ä¡
066 */
067 public int msg_pos = -1;
068
069 /**
070 * ÇöÀç ¿¸° ¸Þ¼¼ÁöÀÇ Ã·ºÎ ÆÄÀÏ
071 */
072 public Vector msg_attaches = new Vector();
073
074 /**
075 * ÇöÀç ¿¸° ¸Þ¼¼ÁöÀÇ Ã·ºÎ ÆÄÀÏÀÇ À§Ä¡
076 */
077 public int msgattach_pos = -1;
078
079
080 /**
081 * ¸ÞÀÏ ÁÖ¼Ò from
082 */
083 public InternetAddress from ;
084
085 /**
086 * SMTP °ü·Ã ȯ°æ ÆÄÀÏ
087 */
088 public rainProperties rainProperties ;
089
090 /**
091 * »ý¼ºÀÚ
092 * @param pro SMTP ȯ°æÆÄÀÏ
093 * @param email ¸ÞÀÏÁÖ¼Ò
094 * @param name À̸§
095 */
096 public rainMail(rainProperties pro, String email, String name) {
097 this.InitMail(pro, email, name);
098 }
099
100 /**
101 * »ý¼ºÀÚ
102 * @param pro SMTP ȯ°æÆÄÀÏ
103 */
104 public rainMail(rainProperties pro) {
105 this.InitMail(pro,
106 pro.getProperty("rainMail-default-email","nobody@localhost"),
107 pro.getProperty("rainMail-default-name","nobody")
108 );
109 }
110
111 /**
112 * ±âº» Á¤º¸ ÃʱâÈ
113 * @param pro SMTP ȯ°æÆÄÀÏ
114 * @param email ¸ÞÀÏÁÖ¼Ò
115 * @param name À̸§
116 */
117 public void InitMail(rainProperties pro, String email, String name) {
118 rainProperties = pro;
119 try{
120 from = new InternetAddress(email ,name);
121 }catch(Exception e){ error_msg += e.getMessage(); }
122 }
123
124 /**
125 * ÷ºÎ ÆÄÀÏ ¼³Á¤
126 * @param server_pos ÷ºÎÇÒ ÆÄÀÏÀÇ ¼¹ö»ó °æ·Î
127 * @param file_name ÷ºÎÇÒ ÆÄÀϸí
128 */
129 public void setAttach(String server_pos, String file_name) {
130 if ((new File(server_pos)).isFile()) this.content_attaches.addElement((new String[] {server_pos, file_name}));
131 }
132
133 /**
134 * ÷ºÎ ÆÄÀÏ ÃʱâÈ
135 */
136 public void clearAttach() {
137 this.content_attaches = new Vector();
138 }
139
140 /**
141 * ¸ÞÀÏ ¹ß¼Û
142 * @param to ¹ÞÀ» »ç¶÷ ¸ÞÀÏ ÁÖ¼Ò
143 * @param subject Á¦¸ñ
144 * @param content ³»¿ë
145 */
146 public boolean send(String to, String subject, String content) {
147 return this.send(new String[] { to }, subject, content);
148 }
149
150 /**
151 * ¸ÞÀÏ ¹ß¼Û
152 * @param to ¹ÞÀ» »ç¶÷ ¸ÞÀÏ ÁÖ¼Ò - ¿©·¯»ç¶÷ÀÌ µ¿½Ã¿¡ ¹ÞÀ» °æ¿ì ¹è¿·Î ÀÔ·Â
153 * @param subject Á¦¸ñ
154 * @param content ³»¿ë
155 */
156 public boolean send(String[] to, String subject, String content) {
157 Address [] email = new Address[to.length];
158 for (int i = 0; i < to.length; i++) {
159 try{ email[i] = new InternetAddress(to[i]); }catch(Exception e){ error_msg += e.getMessage(); }
160 }
161 return send( email , subject, content);
162 }
163
164 /**
165 * ¸ÞÀÏ ¹ß¼Û
166 * @param to ¹ÞÀ» »ç¶÷ ¸ÞÀÏ ÁÖ¼Ò - Address °´Ã¼
167 * @param subject Á¦¸ñ
168 * @param content ³»¿ë
169 */
170 public boolean send(Address to, String subject, String content) {
171 return send(new Address[] { to}, subject, content);
172 }
173
174 /**
175 * ¸ÞÀÏ ¹ß¼Û
176 * @param to ¹ÞÀ» »ç¶÷ ¸ÞÀÏ ÁÖ¼Ò - Address °´Ã¼ ¿©·¯»ç¶÷ÀÌ µ¿½Ã¿¡ ¹ÞÀ» °æ¿ì ¹è¿·Î ÀÔ·Â
177 * @param subject Á¦¸ñ
178 * @param content ³»¿ë
179 * @return Àü¼Û ¼º°ø¿©ºÎ
180 */
181 public boolean send(Address[] to, String subject, String content) {
182 boolean send_result = new Boolean(true);
183
184 Properties props = new Properties();
185 String protocol = rainProperties.getProperty("rainMail-protocol","smtp");
186 props.setProperty("mail.transport.protocol", protocol);
187 props.setProperty("mail."+protocol+".host", rainProperties.getProperty("rainMail-host","localhost"));
188 if (rainProperties.getProperty("rainMail-auth","").equals("true")) {
189 props.setProperty("mail."+protocol+".auth", "true");
190 props.setProperty("mail."+protocol+".port", rainProperties.getProperty("rainMail-port","25"));
191 props.setProperty("mail."+protocol+".user", rainProperties.getProperty("rainMail-user",""));
192 props.setProperty("mail."+protocol+".localhost", rainProperties.getProperty("rainMail-localhost","localhost"));
193 }
194
195 try{
196 Session session = Session.getDefaultInstance(props, null);
197 Transport trans = session.getTransport(protocol);
198 session.setDebug(true);
199
200 Message msg = new MimeMessage(session);
201 msg.setHeader("Content-Type",content_type +"; charset=" +content_charset);
202 msg.setFrom(from);
203 msg.addRecipients(Message.RecipientType.TO, to );
204 msg.setSubject(subject);
205 if (content_attaches.size() == 0) {
206 msg.setContent(content, content_type +"; charset="+content_charset);
207 } else {
208 MimeMultipart multipart = new MimeMultipart("mixed");
209 BodyPart messageBodyPart = new MimeBodyPart();
210 messageBodyPart.setContent(content, content_type +"; charset="+content_charset);
211 multipart.addBodyPart(messageBodyPart);
212 for(int i = 0; i < content_attaches.size(); i++) {
213 String[] content_attach = (String[]) content_attaches.elementAt(i);
214 if (content_attach.length == 2) {
215 messageBodyPart = new MimeBodyPart();
216 DataSource fds = new FileDataSource(content_attach[0]);
217 messageBodyPart.setHeader("Content-Type",fds.getContentType()+"; name=\""+content_attach[1]+"\"");
218 messageBodyPart.setHeader("Content-Disposition","attachment; filename=\""+content_attach[1]+"\"");
219 messageBodyPart.setDataHandler(new DataHandler(fds));
220 multipart.addBodyPart(messageBodyPart);
221 }
222 }
223 clearAttach();
224 msg.setContent(multipart);
225 }
226 if (rainProperties.getProperty("rainMail-auth","").equals("true")) {
227 trans.connect(
228 rainProperties.getProperty("rainMail-host","localhost"),
229 Integer.parseInt(rainProperties.getProperty("rainMail-port","25")),
230 rainProperties.getProperty("rainMail-user",""),
231 rainProperties.getProperty("rainMail-passwd","")
232 );
233 } else {
234 trans.connect();
235 }
236 msg.saveChanges();
237 trans.sendMessage(msg,msg.getAllRecipients());
238 trans.close();
239
240 }catch(Exception e){ error_msg += e.getMessage(); send_result = new Boolean(false); }
241 return send_result;
242 }
243
244 /**
245 * ¸ÞÀÏ Æúµå °¡Á®¿À±â
246 */
247 public void getFolders() {
248 try{
249 if (connect() && folders == null) {
250 Folder folder = store.getDefaultFolder();
251 if(folder.exists()) {
252 folders = folder.list();
253 }
254 }
255 } catch(MessagingException e){ error_msg += e.getMessage();}
256 }
257
258 /**
259 * ´ÙÀ½ Æúµå °¡Á®¿À±â
260 * @return À̵¿ ¼º°ø¿©ºÎ
261 */
262 public boolean nextFolder() {
263 if (folders == null || folders.length == 0) return false;
264 else if (folders.length <= (folders_pos +1)) return false;
265 else {
266 folders_pos++;
267 return true;
268 }
269 }
270
271 /**
272 * ù¹øÂ° Æúµå·Î À̵¿
273 */
274 public void firstFolder() {
275 folders_pos = 0;
276 }
277
278 /**
279 * ¸¶Áö¸· Æúµå·Î À̵¿
280 */
281 public void lastFolder() {
282 folders_pos = folders.length -1;
283 }
284
285 /**
286 * ÇöÀç ¿¸° ÆúµåÀÇ Æ¯Á¤ Á¤º¸ °¡Á®¿À±â
287 * @param field_nm Á¤º¸À̸§(name : Æúµå¸í, fullname : Ç®³×ÀÓ, msgcount : ¸Þ¼¼Áö¼ö, newcount : »õ·Î¿î¸Þ¼¼Áö¼ö, unreadcount : ÀÐÁö ¾ÊÀº ¸Þ¼¼Áö¼ö, hasnew : »õ·Î¿î¸Þ¼¼ÁöÀ¯¹«[Y|N]
288 * @return Çʵ尪
289 */
290 public String getFolder(String field_nm) {
291 String ret_val = "";
292 try {
293 if (folders_pos > -1 && folders_pos < folders.length) {
294 Folder folder = folders[folders_pos];
295 if (field_nm.equals("name")) ret_val = folder.getName();
296 else if (field_nm.equals("fullname")) ret_val = folder.getFullName();
297 else if (field_nm.equals("msgcount")) ret_val = Integer.toString(folder.getMessageCount());
298 else if (field_nm.equals("newcount")) ret_val = Integer.toString(folder.getNewMessageCount());
299 else if (field_nm.equals("unreadcount")) ret_val = Integer.toString(folder.getUnreadMessageCount());
300 else if (field_nm.equals("hasnew")) ret_val = (folder.hasNewMessages())?"Y":"N";
301 }
302 } catch(Exception e){ error_msg += e.getMessage();}
303 return ret_val;
304 }
305
306 /**
307 * »õ·Î¿î Æúµå¸¦ »ý¼ºÇÔ
308 * @param folder_nm Æúµå À̸§
309 * @return »ý¼º¼º°ø¿©ºÎ
310 */
311 public boolean mkFolder(String folder_nm) {
312 boolean ret_bl = new Boolean(true);
313 try {
314 if (openFolder("/")) {
315 Folder folder = store.getFolder(folder_nm);
316 if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES);
317 else ret_bl = new Boolean(false);
318 } else ret_bl = new Boolean(false);
319 } catch(Exception e){ error_msg += e.getMessage();}
320 return ret_bl;
321 }
322
323 /**
324 * Æúµå¸¦ »èÁ¦ÇÔ
325 * @param folder_nm Æúµå À̸§
326 * @return »èÁ¦¼º°ø¿©ºÎ
327 */
328 public boolean delFolder(String folder_nm) {
329 boolean ret_bl = new Boolean(true);
330 try {
331 if (openFolder("/")) {
332 Folder folder = store.getFolder(folder_nm);
333 if (folder.exists()) folder.delete(new Boolean(true));
334 else ret_bl = new Boolean(false);
335 } else ret_bl = new Boolean(false);
336 } catch(Exception e){ error_msg += e.getMessage();}
337 return ret_bl;
338 }
339
340 /**
341 * ÆúµåÀ̸§À» º¯°æÇÔ
342 * @param old_nm ±âÁ¸À̸§
343 * @param new_nm »õ·Î¿îÀ̸§
344 * @return º¯°æ¼º°ø¿©ºÎ
345 */
346 public boolean renFolder(String old_nm, String new_nm) {
347 boolean ret_bl = new Boolean(true);
348 try {
349 if (openFolder("/")) {
350 Folder old_folder = store.getFolder(old_nm);
351 Folder new_folder = store.getFolder(new_nm);
352
353 if (old_folder.exists() && !new_folder.exists()) old_folder.renameTo( new_folder );
354 else ret_bl = new Boolean(false);
355 } else ret_bl = new Boolean(false);
356 } catch(Exception e){ error_msg += e.getMessage();}
357 return ret_bl;
358 }
359
360 /**
361 * Æúµå¿±â
362 * @param folder_nm ¿¾îµÑ Æúµå¸í
363 * @return ¿±â¼º°ø¿©ºÎ
364 */
365 public boolean openFolder(String folder_nm) {
366 boolean ret_bl = new Boolean(true);
367 try {
368 if (connect()) {
369 Folder folder = null;
370 if (folder_nm.equals("/")) folder = store.getDefaultFolder();
371 else folder = store.getFolder(folder_nm);
372 if (!folder.exists()) {
373 ret_bl = new Boolean(false);
374 } else if (open_folder == null || !open_folder.equals(folder)) {
375 closeFolder();
376 folder.open(Folder.READ_WRITE);
377 open_folder = folder;
378 } else ret_bl = new Boolean(false);
379 } else ret_bl = new Boolean(false);
380 } catch(MessagingException e){ error_msg += e.getMessage();}
381 return ret_bl;
382 }
383
384 /**
385 * Æúµå´Ý±â
386 */
387 public void closeFolder() {
388 try {
389 if (open_folder != null) open_folder.close(new Boolean(false));
390 open_folder = null;
391 } catch(Exception e){ error_msg += e.getMessage();}
392 }
393
394 /**
395 * ÇØ´ç Æúµå¿¡¼ ¸Þ¼¼Áö ÀûÀçÇϱâ
396 * @param folder_nm ¿¾îµÑ Æúµå¸í
397 * @return ÀûÀ缺°ø¿©ºÎ
398 */
399 public boolean getMessages(String folder_nm) {
400 boolean ret_bl = new Boolean(true);
401 try {
402 if (openFolder(folder_nm)) {
403 messages = open_folder.getMessages();
404 } else ret_bl = new Boolean(false);
405 } catch(Exception e){ error_msg += e.getMessage();}
406 return ret_bl;
407 }
408
409 /**
410 * ÇØ´ç Æúµå¿¡¼ ƯÁ¤ ¹øÈ£ ¸Þ¼¼Áö ÀûÀçÇϱâ
411 * @param folder_nm ¿¾îµÑ Æúµå¸í
412 * @param msgno ¸Þ¼¼Áö¹øÈ£
413 * @return ÀûÀ缺°ø¿©ºÎ
414 */
415 public boolean getMessage(String folder_nm, int msgno) {
416 boolean ret_bl = new Boolean(true);
417 try {
418 if (openFolder(folder_nm)) {
419 messages = new Message[] {open_folder.getMessage(msgno)};
420 } else ret_bl = new Boolean(false);
421 } catch(Exception e){ error_msg += e.getMessage();}
422 return ret_bl;
423 }
424
425 /**
426 * ´ÙÀ½ ¸Þ¼¼Áö·Î À̵¿
427 * @return À̵¿ ¼º°ø¿©ºÎ
428 */
429 public boolean nextMsg() {
430 if (messages == null || messages.length == 0) return false;
431 else if (messages.length <= (msg_pos +1)) return false;
432 else {
433 msg_pos++;
434 return true;
435 }
436 }
437
438 /**
439 * ù¹øÂ° ¸Þ¼¼Áö·Î À̵¿
440 */
441 public void firstMsg() {
442 msg_pos = 0;
443 }
444
445 /**
446 * ¸¶Áö¸· ¸Þ¼¼Áö·Î À̵¿
447 */
448 public void lastMsg() {
449 msg_pos = messages.length -1;
450 }
451
452 /**
453 * ÇöÀç ÀûÀçµÈ ¸Þ¼¼Áö¿¡¼ ƯÀû Çʵ尪°¡Á®¿À±â
454 * @param field_nm Çʵå¸í (subject:Á¦¸ñ, msgno : ¸Þ¼¼Áö¹øÈ£, contenttype : ŸÀÙ, content : ³»¿ë)
455 * @return Çʵ尪
456 */
457 public String getMsginfo(String field_nm) {
458 String ret_val = "";
459 try {
460 if (msg_pos > -1 && msg_pos < messages.length && messages[msg_pos] != null ) {
461 Message message = messages[msg_pos];
462 if (field_nm.equals("subject")) ret_val = message.getSubject();
463 else if (field_nm.equals("msgno")) ret_val = Integer.toString(message.getMessageNumber());
464 else if (field_nm.equals("contenttype")) ret_val = message.getContentType();
465 else if (field_nm.equals("content")) {
466 msg_attaches = new Vector();
467 msgattach_pos = -1;
468 if(message.isMimeType("multipart/*")) {
469 Multipart multipart = (Multipart)message.getContent();
470 for(int i = 0;i < multipart.getCount();i++) {
471 Part p = multipart.getBodyPart(i);
472 if(p.isMimeType("text/plain")) ret_val += p.getContent();
473 else msg_attaches.addElement(p);
474 }
475 } else if(message.isMimeType("text/plain")) {
476 ret_val = (String) message.getContent();
477 } else ret_val = (String) message.getContent();
478
479 }
480 }
481 } catch(Exception e){ error_msg += e.getMessage();}
482 return ret_val;
483 }
484
485 /**
486 * ¿¸° ¸Þ¼¼Áö¿¡¼ ÷ºÎÆÄÀÏ °¡Á®¿À±â
487 * @param field_nm Çʵå¸í (filename:ÆÄÀϸí, filesize : ÆÄÀÏÅ©±â, data : ÆÄÀϰ´Ã¼)
488 * @return Çʵ尪
489 */
490 public Object getMsgattach(String field_nm) {
491 Object ret_val = null;
492 try {
493 Part p = (Part) msg_attaches.elementAt(msgattach_pos);
494 if (field_nm.equals("filename")) ret_val = p.getFileName();
495 else if (field_nm.equals("filesize")) ret_val = p.getSize();
496 else if (field_nm.equals("data")) ret_val = p.getDataHandler();
497 } catch(Exception e){ error_msg += e.getMessage();}
498 if (ret_val == null ) ret_val = "";
499 return ret_val;
500 }
501
502 /**
503 * ¿¸° ¸Þ¼¼Áö¿¡¼ ÷ºÎÆÄÀÏ ´Ù¿î·ÎµåÇϱâ
504 * @param response HttpServletResponse
505 * @param file_nm ´Ù¿î·ÎµåÇÒ ÆÄÀϸí
506 */
507 public void getMsgattachdown(HttpServletResponse response, String file_nm) {
508 try {
509 for (int i = 0 ; i < msg_attaches.size(); i++) {
510 Part p = (Part) msg_attaches.elementAt(i);
511 error_msg += p.getFileName();
512 if (p != null && p.getFileName().trim().equals(file_nm)) {
513 error_msg += file_nm;
514 response.setContentType("Content-type: application/octet-stream;");
515 response.setHeader("Content-Disposition", "attachment;filename=" + p.getFileName() + ";");
516 p.getDataHandler().writeTo(response.getOutputStream());
517 }
518 }
519 } catch(Exception e){ error_msg += e.getMessage();}
520 }
521
522 /**
523 * ´ÙÀ½ ÷ºÎµÈ ÆÄÀÏ ¿±â
524 * @return ¿±â ¼º°ø¿©ºÎ
525 */
526 public boolean nextMsgattach() {
527 if (msg_attaches == null || msg_attaches.size() == 0) return false;
528 else if (msg_attaches.size() <= (msgattach_pos +1)) return false;
529 else {
530 msgattach_pos++;
531 return true;
532 }
533 }
534
535 /**
536 * ù ÷ºÎÆÄÀÏ·Î À̵¿
537 */
538 public void firstMsgattach() {
539 msgattach_pos = 0;
540 }
541
542 /**
543 * ¸¶Áö¸· ÷ºÎÆÄÀÏ·Î À̵¿
544 */
545 public void lastMsgattach() {
546 msgattach_pos = msg_attaches.size() -1;
547 }
548
549 /**
550 * SMTP ¼¹ö Á¢¼Ó
551 * @return Á¢¼Ó ¼º°ø¿©ºÎ
552 */
553 public boolean connect() {
554 if (store == null || !store.isConnected()) {
555 Properties props = new Properties();
556 String protocol = rainProperties.getProperty("rainMail-protocol","smtp");
557 props.setProperty("mail.transport.protocol", protocol);
558 props.setProperty("mail."+protocol+".host", rainProperties.getProperty("rainMail-host","localhost"));
559 if (rainProperties.getProperty("rainMail-auth","").equals("true")) {
560 props.setProperty("mail."+protocol+".auth", "true");
561 props.setProperty("mail."+protocol+".port", rainProperties.getProperty("rainMail-port","25"));
562 props.setProperty("mail."+protocol+".user", rainProperties.getProperty("rainMail-user",""));
563 props.setProperty("mail."+protocol+".localhost", rainProperties.getProperty("rainMail-localhost","localhost"));
564 }
565
566 try{
567 Session session = Session.getDefaultInstance(props, null);
568 session.setDebug(true);
569 store = session.getStore(protocol);
570 if (rainProperties.getProperty("rainMail-auth","").equals("true")) {
571 store.connect(
572 rainProperties.getProperty("rainMail-host","localhost"),
573 Integer.parseInt(rainProperties.getProperty("rainMail-port","25")),
574 rainProperties.getProperty("rainMail-user",""),
575 rainProperties.getProperty("rainMail-passwd","")
576 );
577 } else {
578 store.connect();
579 }
580 } catch(Exception e){ error_msg += e.getMessage();}
581 }
582 if (store == null || !store.isConnected()) return new Boolean(false);
583 return new Boolean(true);
584 }
585
586 /**
587 * SMTP ¼¹ö ÇØÁ¦
588 */
589 public void close() {
590 try {
591 if (store.isConnected()) store.close();
592 } catch(Exception e){ error_msg += e.getMessage();}
593 }
594 }
595