1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.portlets.email;
18
19
20
21 import javax.mail.Session;
22 import javax.mail.Store;
23 import javax.mail.Folder;
24 import javax.mail.AuthenticationFailedException;
25 import javax.mail.NoSuchProviderException;
26 import javax.mail.Message;
27 import javax.mail.Transport;
28 import javax.mail.Address;
29 import javax.mail.Multipart;
30 import javax.mail.Flags;
31 import javax.mail.Part;
32 import javax.mail.Header;
33
34 import javax.mail.internet.MimeMessage;
35 import javax.mail.internet.InternetAddress;
36 import javax.mail.internet.MimeBodyPart;
37 import javax.mail.internet.MimeMultipart;
38
39 import javax.activation.DataSource;
40 import javax.activation.FileDataSource;
41 import javax.activation.DataHandler;
42
43 import com.sun.mail.imap.IMAPFolder;
44
45 import org.apache.turbine.util.upload.FileItem;
46 import org.apache.turbine.services.servlet.TurbineServlet;
47
48 import java.io.File;
49
50
51 import java.util.List;
52 import java.util.Enumeration;
53 import java.util.Properties;
54 import java.util.Vector;
55 import java.util.Hashtable;
56
57
58 import org.apache.torque.util.Criteria;
59
60 import org.apache.jetspeed.om.apps.email.EmailInboxPeer;
61 import org.apache.jetspeed.om.apps.email.EmailInbox;
62
63
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66
67 public class Email
68 {
69
70 private static Log log = LogFactory.getLog(Email.class);
71
72 private Properties props;
73
74 private Session session;
75
76 private Store store;
77
78 private Folder folder;
79
80 private Hashtable parameters;
81
82 /***
83 * Email Action
84 *
85 * @author <a href="mailto:jlim@gluecode.com">Jonas Lim </a>
86 * @version $Id: Email.java,v 1.2 2004/03/22 22:26:58 taylor Exp $
87 */
88 public Email(String user, String pass, Hashtable param)
89 throws AuthenticationFailedException, NoSuchProviderException,
90 Exception
91 {
92
93 parameters = param;
94 String host = (String) parameters.get("hostname");
95 String protocol = (String) parameters.get("protocol");
96 String smtpUser = (String) parameters.get("smtp_user");
97 String smtpPort = (String) parameters.get("smtp_port");
98 String smtpFrom = (String) parameters.get("smtp_from");
99 String smtpConnTimeout = (String) parameters.get("smtp_conn_timeout");
100 String smptTimeout = (String) parameters.get("smtp_timeout");
101 String smtpLocalhost = (String) parameters.get("smtp_localhost");
102 String smtpEhlo = (String) parameters.get("smtp_ehlo");
103 String smtpAuth = (String) parameters.get("smtp_auth");
104 String smtpDSNNotify = (String) parameters.get("smtp_dsn_notify");
105 String smtpDSNRet = (String) parameters.get("smtp_dsn_ret");
106 String smtpallow8bitmime = (String) parameters
107 .get("smtp_allow8bitmime");
108 String smtpsendPartial = (String) parameters.get("smtp_send_partial");
109 String smtpSaslrealm = (String) parameters.get("smtp_sasl_realm");
110 String smtpquitWait = (String) parameters.get("smtp_quit_wait");
111 String imapPort = null;
112 String imapPartialfetch = null;
113 String imapFetchsize = null;
114 String imapTimeout = null;
115 String imapHost = null;
116
117 if (protocol.equals("imap"))
118 {
119 imapPort = (String) parameters.get("imap_port");
120 imapPartialfetch = (String) parameters.get("imap_partial_fetch");
121 imapFetchsize = (String) parameters.get("imap_fetch_size");
122 imapTimeout = (String) parameters.get("imap_timeout");
123 imapHost = (String) parameters.get("imap_host");
124 }
125
126 props = new Properties();
127 props.put("mail.smtp.host", host);
128 if (!smtpUser.equals(""))
129 {
130 props.put("mail.smtp.user", smtpUser);
131 }
132 if (!smtpPort.equals(""))
133 {
134 props.put("mail.smtp.port", smtpPort);
135 }
136 if (!smtpFrom.equals(""))
137 {
138 props.put("mail.smtp.from", smtpFrom);
139 }
140 if (!smtpConnTimeout.equals(""))
141 {
142 props.put("mail.smtp.connectiontimeout", smtpConnTimeout);
143 }
144 if (!smptTimeout.equals(""))
145 {
146 props.put("mail.smtp.timeout", smptTimeout);
147 }
148 if (!smtpLocalhost.equals(""))
149 {
150 props.put("mail.smtp.localhost", smtpLocalhost);
151 }
152 if (!smtpEhlo.equals(""))
153 {
154 props.put("mail.smtp.ehlo", smtpEhlo);
155 }
156 if (!smtpAuth.equals(""))
157 {
158 props.put("mail.smtp.auth", smtpAuth);
159 }
160 if (!smtpDSNNotify.equals(""))
161 {
162 props.put("mail.smtp.dsn.notify", smtpDSNNotify);
163 }
164 if (!smtpDSNRet.equals(""))
165 {
166 props.put("mail.smtp.dsn.ret", smtpDSNRet);
167 }
168 if (!smtpallow8bitmime.equals(""))
169 {
170 props.put("mail.smtp.allow8bitmime", smtpallow8bitmime);
171 }
172 if (!smtpsendPartial.equals(""))
173 {
174 props.put("mail.smtp.sendpartial", smtpsendPartial);
175 }
176 if (!smtpSaslrealm.equals(""))
177 {
178 props.put("mail.smtp.saslrealm", smtpSaslrealm);
179 }
180 if (!smtpquitWait.equals(""))
181 {
182 props.put("mail.smtp.quitwait", smtpquitWait);
183 }
184
185 if (protocol.equals("imap"))
186 {
187 if ((imapPort != null) && (!imapPort.equals("")))
188 {
189 props.put("mail.imap.port", imapPort);
190 }
191 if ((imapPartialfetch != null) && (!imapPartialfetch.equals("")))
192 {
193 props.put("mail.imap.partialfetch", imapPartialfetch);
194 }
195 if ((imapFetchsize != null) && (!imapFetchsize.equals("")))
196 {
197 props.put("mail.imap.fetchsize", imapFetchsize);
198 }
199 if ((imapTimeout != null) && (!imapTimeout.equals("")))
200 {
201 props.put("mail.imap.timeout", imapTimeout);
202 }
203 if ((imapHost != null) && (!imapHost.equals("")))
204 {
205 props.put("mail.imap.host", imapHost);
206 }
207 }
208
209 session = Session.getDefaultInstance(props, null);
210
211
212 store = session.getStore(protocol);
213
214
215 store.connect(host, user, pass);
216
217 folder = store.getFolder("INBOX");
218
219 }
220
221 public void authenticateUser(String user, String pass)
222 throws AuthenticationFailedException, NoSuchProviderException,
223 Exception
224 {
225 String protocol = (String) parameters.get("protocol");
226 String host = (String) parameters.get("hostname");
227
228 Properties props = new Properties();
229 props.put("mail.smtp.host", host);
230
231
232 Session session = Session.getDefaultInstance(props, null);
233
234
235 Store store = session.getStore(protocol);
236 store.connect(host, user, pass);
237
238
239 }
240
241 public void doSendEmail(String addressTo, String addressFrom,
242 String subject, String msg, FileItem file) throws Exception
243 {
244
245 Session session = Session.getDefaultInstance(props, null);
246 Message message = new MimeMessage(session);
247 message.setFrom(new InternetAddress(addressFrom));
248
249 checkRecipients(addressTo, message);
250
251
252 if (subject.equals(""))
253 {
254 subject = "none";
255 }
256
257 message.setSubject(subject);
258
259
260
261 if (file != null && !file.equals(""))
262 {
263 sendAttachment(message, file, msg);
264 } else
265 {
266 log.info("file null or space " + file);
267 message.setText(msg);
268 }
269
270 Transport.send(message);
271
272 if (file != null)
273 {
274
275 }
276 }
277
278 public void uploadAttachment(FileItem fileItem) throws Exception
279 {
280
281 log.info("upload attachment");
282
283 java.io.File file1 = new java.io.File(fileItem.getFileName());
284
285 String filePath = file1.getAbsolutePath();
286 int d = filePath.lastIndexOf(File.separator);
287 String b = filePath.substring(d + 1);
288 String filename = b;
289
290 fileItem.write(getAttachmentsFolder() + File.separator + filename);
291
292 }
293
294 public void deleteUploadedfile(FileItem file) throws Exception
295 {
296 {
297 log.info("delete uploaded file");
298 java.io.File fn = new java.io.File(file.getFileName());
299 String filename = fn.getName();
300 String realPath = getAttachmentsFolder();
301
302 File fDelete = new File(realPath + File.separator + filename);
303 System.out.println("deleted file : " + fDelete);
304 fDelete.delete();
305 }
306
307 }
308
309
310 public void checkRecipients(String addressTo, Message message)
311 throws Exception
312 {
313 String recipient = null;
314 int startIndex = 0;
315 int semicolonIndex = 0;
316 int lastsemicolonIndex = 0;
317
318 try
319 {
320 if (addressTo.indexOf(";", 0) == -1)
321 {
322 log.info("addr" + addressTo.indexOf(";", 0));
323 message.setRecipients(Message.RecipientType.TO, InternetAddress
324 .parse(addressTo, false));
325 } else
326 {
327 while ((semicolonIndex = addressTo.indexOf(";", startIndex)) != -1)
328 {
329 recipient = addressTo.substring(startIndex, semicolonIndex);
330 startIndex = semicolonIndex + 1;
331 lastsemicolonIndex = semicolonIndex;
332 message.addRecipient(Message.RecipientType.TO,
333 new InternetAddress(recipient));
334
335 }
336 recipient = addressTo.substring(lastsemicolonIndex + 1);
337 message.addRecipient(Message.RecipientType.TO,
338 new InternetAddress(recipient));
339 log.info("recipient" + recipient);
340 }
341 Address a[] = message.getAllRecipients();
342 for (int j = 0; j < a.length; j++)
343 {
344 log.info("address" + a[j]);
345 }
346
347 } catch (Exception e)
348 {
349 log.error("Error in checkRecepients()",e);
350 }
351 }
352
353 public void sendAttachment(Message message, FileItem file, String msg)
354 throws Exception
355 {
356
357 log.info("file not null or space " + file);
358
359 uploadAttachment(file);
360
361 java.io.File fn = new java.io.File(file.getFileName());
362 String filename = fn.getName();
363
364
365 MimeBodyPart messageBodyPart = new MimeBodyPart();
366
367 messageBodyPart.setContent(msg, "text/html");
368
369 MimeBodyPart messageBodyPart2 = new MimeBodyPart();
370
371 DataSource source = new FileDataSource(getAttachmentsFolder()
372 + File.separator + filename);
373
374
375 messageBodyPart2.setDataHandler(new DataHandler(source));
376
377
378 messageBodyPart2.setFileName(filename.toString());
379
380
381 Multipart multipart = new MimeMultipart();
382 multipart.addBodyPart(messageBodyPart);
383 multipart.addBodyPart(messageBodyPart2);
384
385
386 message.setContent(multipart);
387
388 }
389
390 public void reply(String from, String addressTo, String msgecontent,
391 String subject, FileItem file, Message msge) throws Exception
392 {
393
394
395
396
397
398
399
400
401 MimeMessage message = (MimeMessage) msge.reply(false);
402
403 message.setFrom(new InternetAddress(from));
404
405 checkRecipients(addressTo, message);
406
407 message.setSubject(subject);
408
409 message.setContent(msgecontent, "text/html");
410
411 if (file != null && !file.equals(""))
412 {
413 log.info("reply with attachment ******");
414 sendAttachment(message, file, msgecontent);
415 }
416
417 Transport.send(message);
418
419 if (file != null)
420 {
421 deleteUploadedfile(file);
422 }
423
424
425
426 }
427
428 public void forward(String to, String from, String subject, String content,
429 Message message) throws Exception
430 {
431
432
433
434
435
436
437
438 Message forward = new MimeMessage(session);
439
440 forward.setFrom(new InternetAddress(from));
441 forward.setSubject(subject);
442
443 checkRecipients(to, forward);
444
445 MimeMessage orig = (MimeMessage) message;
446 if (orig.isMimeType("text/plain"))
447 {
448 log.info("text/plain forward");
449 forward.setText(content.toString());
450 } else
451 {
452 log.info("forward html *******************");
453 forward.setContent(content, "text/html");
454 }
455
456 Transport.send(forward);
457
458
459 }
460
461
462 public void contentDelete(int current_index) throws Exception
463 {
464 if(!folder.isOpen())
465 {
466 folder.open(Folder.READ_WRITE);
467 }
468
469
470 Message message[] = folder.getMessages();
471 message[current_index].setFlag(Flags.Flag.DELETED, true);
472 log.info("index" + current_index);
473 }
474
475
476 public void contentDelete(int current_index, String foldername,
477 String protocol) throws Exception
478 {
479
480 Folder folder_name = store.getFolder(foldername);
481 folder_name.open(Folder.READ_WRITE);
482
483 Message fmsge[] = folder_name.getMessages();
484 fmsge[current_index].setFlag(Flags.Flag.DELETED, true);
485 if (protocol.equals("imap"))
486 {
487 folder_name.expunge();
488 }
489
490 }
491
492
493 public void checkboxDelete(String foldername, String[] checkboxes,
494 String protocol) throws Exception
495 {
496 Folder folder_name = store.getFolder(foldername);
497 folder_name.open(Folder.READ_WRITE);
498
499 Message message[] = folder_name.getMessages();
500
501 for (int i = 0; i < checkboxes.length; i++)
502 {
503 int ind = Integer.parseInt(checkboxes[i]);
504 message[ind].setFlag(Flags.Flag.DELETED, true);
505 if (protocol.equals("imap"))
506 {
507 folder_name.expunge();
508 }
509 }
510 }
511
512
513 public void checkboxDelete(String[] checkboxes) throws Exception
514 {
515 if(!folder.isOpen())
516 {
517 folder.open(Folder.READ_WRITE);
518 }
519
520
521 Message message[] = folder.getMessages();
522
523 for (int i = 0; i < checkboxes.length; i++)
524 {
525 int ind = Integer.parseInt(checkboxes[i]);
526 message[ind].setFlag(Flags.Flag.DELETED, true);
527
528 }
529 }
530
531
532 public int getNo_of_messages() throws Exception
533 {
534
535 if(!folder.isOpen())
536 {
537 folder.open(Folder.READ_WRITE);
538 }
539
540 Vector vMessages = new Vector();
541 Message message[] = folder.getMessages();
542
543 int msgectr = message.length;
544 return msgectr;
545
546 }
547
548
549 public int num_Newmessages() throws Exception
550 {
551
552 if(!folder.isOpen())
553 {
554 folder.open(Folder.READ_ONLY);
555 }
556
557
558 Message message[] = folder.getMessages();
559 Vector vNewmessages = new Vector();
560
561 int length = message.length;
562 for (int i = 0; i < length; i++)
563 {
564
565 folder.close(true);
566 Message current_message = getMessage(i);
567 int newMessage = checkNewmessage(current_message);
568 if (newMessage == 1)
569 {
570 vNewmessages.add(String.valueOf(current_message
571 .getMessageNumber()));
572 log.info("@@@@@@@@@@@@@@@@@@@@@@@@@ new!!!");
573 } else
574 log.info("@@@@@@@@@@@@@@@@@@@@@@@@@ old");
575 }
576 int num_newMessages = vNewmessages.size();
577 return num_newMessages;
578 }
579
580
581
582
583
584 public void close() throws Exception
585 {
586
587 if(folder.isOpen() && folder != null)
588 {
589 folder.close(true);
590 }
591
592 if(store.isConnected() && folder != null)
593 {
594 store.close();
595 }
596 }
597
598 public void storeClose() throws Exception
599 {
600 if(store.isConnected() && store != null)
601 {
602 store.close();
603 }
604
605 }
606
607 public void close(Folder foldername) throws Exception
608 {
609
610 if(folder.isOpen() && folder != null)
611 {
612 folder.close(true);
613 }
614
615 if(store.isConnected() && store != null)
616 {
617 store.close();
618 }
619 }
620
621 public Vector openInbox(String protocol) throws Exception
622 {
623
624 if(!folder.isOpen())
625 {
626 folder.open(Folder.READ_ONLY);
627 }
628
629 Vector vAscmessages = new Vector();
630 Message message[] = folder.getMessages();
631
632 for (int i = 0; i < message.length; i++)
633 {
634
635 Message msge = message[i];
636
637 boolean withAttachment = checkAttachment(msge);
638 Hashtable ht = new Hashtable();
639 ht.put("From", msge.getFrom()[0]);
640 if (msge.getSubject() == null)
641 {
642 ht.put("Subject", "none");
643
644 } else
645 {
646 ht.put("Subject", msge.getSubject());
647 }
648 ht.put("index", String.valueOf(i));
649
650
651
652
653
654
655
656
657 if (protocol.equals("imap"))
658 {
659 ht.put("ReceivedDate", msge.getReceivedDate());
660 } else
661 {
662 log.info("empty date!!!!!!!");
663 ht.put("ReceivedDate", "");
664 }
665
666 ht.put("size", String.valueOf(msge.getSize()));
667 ht.put("message", msge);
668 if (withAttachment == true)
669 {
670 ht.put("hasAttachment", "Attachment");
671 } else
672 {
673 ht.put("hasAttachment", "");
674 }
675
676 int status = checkNewmessage(msge);
677 log.info("status " + status);
678
679 if (status == 1)
680 {
681 ht.put("status", "new");
682 } else
683 {
684 ht.put("status", "");
685 }
686 vAscmessages.add(ht);
687 }
688
689
690 return vAscmessages;
691 }
692
693
694 public Message getMessage(int current_index) throws Exception
695 {
696
697 if(!folder.isOpen())
698 {
699 folder.open(Folder.READ_ONLY);
700 }
701
702 Message[] messages = folder.getMessages();
703 Message message = messages[current_index];
704
705 return message;
706
707 }
708
709
710 public Message getMessage(int current_index, String foldername)
711 throws Exception
712 {
713
714 Folder folder_name = store.getFolder(foldername);
715 folder_name.open(Folder.READ_ONLY);
716 Message[] messages = folder_name.getMessages();
717 Message message = messages[current_index];
718
719 return message;
720
721 }
722
723 public String getSubject(Message message) throws Exception
724 {
725
726 String subject = message.getSubject();
727 return subject;
728 }
729
730 public String getFrom(Message message) throws Exception
731 {
732 String from = message.getFrom()[0].toString();
733 return from;
734 }
735
736 public String getReceivedDate(Message message) throws Exception
737 {
738 String receivedDate = message.getReceivedDate().toString();
739 return receivedDate;
740
741 }
742
743
744 public Vector getTo(Message message) throws Exception
745 {
746 Vector vAddr = new Vector();
747
748 for (int ctr = 0; ctr < message.getRecipients(Message.RecipientType.TO).length; ctr++)
749 {
750 log.info("allRecipients in showcontent"
751 + message.getRecipients(Message.RecipientType.TO)[ctr]);
752 vAddr.add(message.getRecipients(Message.RecipientType.TO)[ctr]);
753 log.info("*** all recipients " + vAddr.size());
754 }
755 return vAddr;
756 }
757
758 public String getMessageContent(Message message) throws Exception
759 {
760 String cmessage = null;
761
762 if (message.isMimeType("text/plain"))
763 {
764
765 cmessage = message.getContent().toString();
766 } else if (message.isMimeType("text/html"))
767 {
768 cmessage = convertMessage(message.getContent().toString());
769 } else
770 {
771 Object obj = message.getContent();
772 Multipart mpart = (Multipart) obj;
773 for (int i = 0, n = mpart.getCount(); i < n; i++)
774 {
775 Vector vAttachments = new Vector();
776
777 Part part = mpart.getBodyPart(i);
778
779 String disposition = part.getDisposition();
780 if ((disposition != null)
781 && ((disposition.equalsIgnoreCase(Part.ATTACHMENT)) || (disposition
782 .equals(Part.INLINE))))
783 {
784 if (disposition.equals(Part.INLINE))
785 {
786 if (part.isMimeType("text/plain"))
787 {
788 cmessage = (String) part.getContent().toString();
789 } else
790 {
791
792 getAttachments(message);
793 }
794 }
795
796 if (disposition.equalsIgnoreCase(Part.ATTACHMENT))
797 {
798 log.info("*** Attachment name: " + part.getFileName());
799 getAttachments(message);
800 }
801 } else if (disposition == null)
802 {
803 if (part.getContent() instanceof MimeMultipart)
804 {
805 MimeMultipart mm = (MimeMultipart) part.getContent();
806 cmessage = (mm.getBodyPart(1)).getContent().toString();
807 } else
808 {
809 cmessage = (String) part.getContent().toString();
810 }
811 }
812 }
813 }
814 return cmessage;
815 }
816
817 public String getAttachmentname(Message message) throws Exception
818 {
819 Object obj = message.getContent();
820 Multipart mpart = (Multipart) obj;
821 String name = null;
822 for (int i = 0, n = mpart.getCount(); i < n; i++)
823 {
824 Part part = mpart.getBodyPart(i);
825 if (part.getFileName() != null) name = part.getFileName();
826 }
827 return name;
828 }
829
830 public Vector getAttachments(Message message) throws Exception
831 {
832
833 Object obj = message.getContent();
834 Multipart mpart = (Multipart) obj;
835
836 Vector vAtt = new Vector();
837 String name = null;
838 String id = null;
839
840 for (int i = 0, n = mpart.getCount(); i < n; i++)
841 {
842 Part part = mpart.getBodyPart(i);
843 name = part.getFileName();
844
845 if (part.getFileName() != null) vAtt.add(part.getFileName());
846
847 }
848 return vAtt;
849 }
850
851 public boolean checkAttachment(Message message) throws Exception
852 {
853 boolean hasattachment = false;
854 try
855 {
856 if ((message.isMimeType("text/plain"))
857 || ((message.isMimeType("text/html"))))
858 {
859 hasattachment = false;
860 }
861
862 else
863 {
864 Object obj = message.getContent();
865 Multipart mpart = (Multipart) obj;
866
867 for (int i = 0, n = mpart.getCount(); i < n; i++)
868 {
869 Part part = mpart.getBodyPart(i);
870
871 String disposition = part.getDisposition();
872
873 if ((disposition != null)
874 && ((disposition.equalsIgnoreCase(Part.ATTACHMENT)) || (disposition
875 .equals(Part.INLINE))))
876 {
877 hasattachment = true;
878 } else if (disposition == null)
879 {
880 if (part.getContent() instanceof MimeMultipart)
881 {
882 hasattachment = true;
883 }
884 } else
885 hasattachment = false;
886 }
887
888 return hasattachment;
889 }
890 } catch (Exception e)
891 {
892 log.error("Error in checkAttachment",e);
893 }
894 return false;
895 }
896
897
898 public int checkNewmessage(Message message) throws Exception
899 {
900 log.info("### check new message in Email");
901
902
903 String msgeId = getMessageId(message);
904
905 Criteria cr = new Criteria();
906 cr.add(EmailInboxPeer.MESSAGE_ID, msgeId);
907
908
909
910 List vMsge = EmailInboxPeer.doSelect(cr);
911 if (vMsge.isEmpty())
912 {
913 return 1;
914 } else
915 {
916
917
918 EmailInbox email = (EmailInbox) EmailInboxPeer.doSelect(cr).get(0);
919
920 if (email.getReadflag() == 0)
921 {
922 return 1;
923 } else
924 return 0;
925 }
926 }
927
928
929
930
931 public String getMessageId(Message message)
932 {
933 String messageid = "";
934 try
935 {
936
937 Enumeration e = message.getAllHeaders();
938 while (messageid.equals(""))
939 {
940 Header header = (Header) e.nextElement();
941 if (header.getName().equals("Message-ID"))
942 {
943 messageid = header.getValue();
944 }
945 }
946 } catch (Exception e)
947 {
948 log.error("Error in getMessageId()",e);
949 }
950 return messageid;
951
952 }
953
954 public String convertMessage(String msg) throws Exception
955 {
956 log.info("convert message");
957
958 StringBuffer sb = new StringBuffer();
959
960 for (int i = 0; i < msg.length(); i++)
961 {
962 char c = msg.charAt(i);
963 if (c == '\n')
964 {
965 log.info("new line");
966 sb.append("<br>");
967 } else
968 {
969 sb.append(c);
970 }
971
972 }
973 String returnString = sb.toString();
974 return returnString;
975 }
976
977 public void doCreatefolder(String folder_name) throws Exception
978 {
979 Folder dfolder = store.getDefaultFolder();
980
981
982 IMAPFolder newfolder = (IMAPFolder) dfolder.getFolder(folder_name);
983
984 if (newfolder == null)
985 {
986
987
988
989
990 }
991
992 if (!newfolder.exists())
993 {
994
995 newfolder.create(Folder.HOLDS_MESSAGES);
996 }
997
998 }
999
1000 public Vector allFolders()
1001 {
1002 Vector vFolders = new Vector();
1003 try
1004 {
1005 Folder allfolders = store.getDefaultFolder();
1006
1007 Folder[] f = allfolders.list();
1008
1009 for (int i = 0; i < f.length; i++)
1010 {
1011
1012 String fn = f[i].getName();
1013
1014 vFolders.add(fn);
1015 }
1016
1017
1018 return vFolders;
1019 } catch (Exception e)
1020 {
1021 log.error("Error in allFolders()",e);
1022 return null;
1023 }
1024 }
1025
1026 public void moveMessage(String fromFolder, String toFolder,
1027 String[] checkboxes)
1028 {
1029 try
1030 {
1031
1032
1033
1034 Folder to_folder = store.getFolder(toFolder);
1035 Folder from_folder = store.getFolder(fromFolder);
1036
1037 from_folder.open(Folder.READ_WRITE);
1038
1039 int tempStart = checkboxes.length;
1040 int startvalue = Integer.parseInt(checkboxes[tempStart - 1]);
1041 int start = startvalue + 1;
1042
1043 int tempEnd = Integer.parseInt(checkboxes[0]);
1044 int end = tempEnd + 1;
1045
1046 to_folder.open(Folder.READ_WRITE);
1047
1048 Message msge[] = from_folder.getMessages(start, end);
1049 from_folder.copyMessages(msge, to_folder);
1050 from_folder.close(true);
1051
1052
1053 String protocol = (String) parameters.get("protocol");
1054
1055 checkboxDelete(fromFolder, checkboxes, protocol);
1056
1057 } catch (Exception e)
1058 {
1059 log.error("Error in moveMessage()",e);
1060 }
1061 }
1062
1063 public void moveMessage(String fromFolder, String toFolder,
1064 int current_index)
1065 {
1066 try
1067 {
1068
1069
1070 Folder dfolder = store.getFolder(toFolder);
1071 Folder from_folder = store.getFolder(fromFolder);
1072
1073 from_folder.open(Folder.READ_WRITE);
1074 dfolder.open(Folder.READ_WRITE);
1075
1076
1077 Message msge[] = from_folder.getMessages(current_index + 1,
1078 current_index + 1);
1079 from_folder.copyMessages(msge, dfolder);
1080
1081
1082
1083 String protocol = (String) parameters.get("protocol");
1084
1085 contentDelete(current_index, fromFolder, protocol);
1086
1087 } catch (Exception e)
1088 {
1089 log.error("Error in Movemessage()",e);
1090 }
1091
1092 }
1093
1094 public Vector openMyfolder(String foldername, String protocol)
1095 throws Exception
1096 {
1097 Vector message_folder = new Vector();
1098
1099 try
1100 {
1101 Folder fname = store.getFolder(foldername);
1102 fname.open(Folder.READ_ONLY);
1103
1104 Message message[] = fname.getMessages();
1105 for (int i = 0; i < message.length; i++)
1106 {
1107 Message msge = message[i];
1108
1109 boolean withAttachment = checkAttachment(msge);
1110 Hashtable ht = new Hashtable();
1111 ht.put("From", msge.getFrom()[0]);
1112
1113 if (msge.getSubject() == null)
1114 {
1115 ht.put("Subject", "none");
1116
1117 } else
1118 {
1119 ht.put("Subject", msge.getSubject());
1120 }
1121 ht.put("index", String.valueOf(i));
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 if (protocol.equals("imap"))
1135 {
1136 ht.put("ReceivedDate", msge.getReceivedDate());
1137 } else
1138 {
1139 log.info("empty date!!!!!!!");
1140 ht.put("ReceivedDate", "");
1141 }
1142
1143 ht.put("size", String.valueOf(msge.getSize()));
1144 ht.put("message", msge);
1145 if (withAttachment == true)
1146 {
1147 ht.put("hasAttachment", "Attachment");
1148 } else
1149 {
1150 ht.put("hasAttachment", "");
1151 }
1152
1153 int status = checkNewmessage(msge);
1154 log.info("status " + status);
1155
1156 if (status == 1)
1157 {
1158 ht.put("status", "new");
1159 } else
1160 {
1161 ht.put("status", "");
1162 }
1163 message_folder.add(ht);
1164 }
1165
1166
1167 return message_folder;
1168
1169 } catch (Exception e)
1170 {
1171 log.error("Error in openMyFolder()",e);
1172 return message_folder;
1173 }
1174
1175 }
1176
1177 public void folderDelete(String folder_name)
1178 {
1179 try
1180 {
1181
1182
1183 Folder current_folder = store.getFolder(folder_name);
1184
1185 current_folder.delete(true);
1186
1187 } catch (Exception e)
1188 {
1189 log.error("Error in folderDelete()",e);
1190 }
1191
1192 }
1193
1194 public Folder getFolder(String folder_name)
1195 {
1196 try
1197 {
1198 Folder current_folder = store.getFolder(folder_name);
1199 return current_folder;
1200 } catch (Exception e)
1201 {
1202 log.error("Error in getFolder()",e);
1203 return null;
1204 }
1205 }
1206
1207 public String getAttachmentsFolder()
1208 {
1209 String path = TurbineServlet.getRealPath(File.separator)
1210 + "attachments";
1211 File aFolder = new File(path);
1212 if (!aFolder.exists())
1213 {
1214 aFolder.mkdir();
1215 }
1216
1217 return path;
1218
1219 }
1220
1221 }