| 1 16 17 package org.apache.jetspeed.modules.actions.portlets.email; 18 19 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 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 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 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 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 session = Session.getDefaultInstance(props, null); 210 store = session.getStore(protocol); 213 214 215 store.connect(host, user, pass); 216 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 Properties props = new Properties (); 229 props.put("mail.smtp.host", host); 230 231 Session session = Session.getDefaultInstance(props, null); 233 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 if (subject.equals("")) 253 { 254 subject = "none"; 255 } 256 257 message.setSubject(subject); 258 259 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 { } 276 } 277 278 public void uploadAttachment(FileItem fileItem) throws Exception  279 { 280 281 log.info("upload attachment"); 282 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 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 MimeBodyPart messageBodyPart = new MimeBodyPart (); 366 messageBodyPart.setContent(msg, "text/html"); 368 369 MimeBodyPart messageBodyPart2 = new MimeBodyPart (); 370 371 DataSource source = new FileDataSource (getAttachmentsFolder() 372 + File.separator + filename); 373 374 messageBodyPart2.setDataHandler(new DataHandler (source)); 376 377 messageBodyPart2.setFileName(filename.toString()); 379 380 Multipart multipart = new MimeMultipart (); 382 multipart.addBodyPart(messageBodyPart); 383 multipart.addBodyPart(messageBodyPart2); 384 385 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 398 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 { deleteUploadedfile(file); 422 } 423 426 } 427 428 public void forward(String to, String from, String subject, String content, 429 Message message) throws Exception  430 { 431 432 436 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 } 460 461 public void contentDelete(int current_index) throws Exception  463 { 464 if(!folder.isOpen()) 465 { 466 folder.open(Folder.READ_WRITE); 467 } 468 469 Message message[] = folder.getMessages(); 471 message[current_index].setFlag(Flags.Flag.DELETED, true); 472 log.info("index" + current_index); 473 } 474 475 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 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 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 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 public void checkboxDelete(String [] checkboxes) throws Exception  514 { 515 if(!folder.isOpen()) 516 { 517 folder.open(Folder.READ_WRITE); 518 } 519 520 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 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 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 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"); } else 645 { 646 ht.put("Subject", msge.getSubject()); 647 } 648 ht.put("index", String.valueOf(i)); 649 650 656 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 int status = checkNewmessage(msge); 677 log.info("status " + status); 678 679 if (status == 1) 680 { ht.put("status", "new"); 682 } else 683 { 684 ht.put("status", ""); 685 } 686 vAscmessages.add(ht); 687 } 688 return vAscmessages; 691 } 692 693 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 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 |