/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = null; Folder fold = null; try { fold = getFolder(); closeFolder(fold); fold = getFolder(); headers = new ArrayList(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(msg.getSubject()); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown(Utility.addressArrToString(header.getFrom())); header.setToShown(Utility.addressArrToString(header.getTo())); header.setCcShown(Utility.addressArrToString(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); // it is time to add it to the arraylist headers.add(header); } catch (MessagingException e1) { System.out.println( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted."); } } } catch (Exception e) { System.out.println("Could not fetch message headers. Is mbox connection still alive???"); throw new ConnectionException(e); } return headers; }
/** * The doPost method of the servlet. <br> * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Expires", "-1"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-control", "no-cache"); response.setHeader("Content-Type", "text/html; charset=utf-8"); request.setCharacterEncoding("UTF-8"); ArrayList parts = (ArrayList) request.getSession().getAttribute("attachments"); if (parts == null) { parts = new ArrayList(); } String fileName = null; try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setRepository(new File(tmpDir)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(MAX_SIZE); // Parse the request List items = upload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { // String fieldName = item.getFieldName(); fileName = item.getName(); // ie6 bug. it sends whole file path as file name. use firefox! if (fileName.indexOf("\\") >= 0) { fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); } File uploadDir = WebdiskController.getUploadDir(getAuthProfile(request).getUsername()); File uploadedFile = new File(uploadDir.getAbsolutePath() + "/" + fileName); item.write(uploadedFile); uploadedFile = null; if (fileName.indexOf("\\") > 0) { fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); } item.delete(); } } response.sendRedirect("../upload_file_ok.jsp?result=0"); } catch (SizeLimitExceededException e) { // attachment exceeded the attachment upload size limit response.sendRedirect( "../upload_file_ok.jsp?result=1&maxAttSize=" + Utility.sizeToHumanReadable(MAX_SIZE)); } catch (Exception e) { response.sendRedirect("../upload_file_ok.jsp?result=3"); } }
/** * The doPost method of the servlet. <br> * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Expires", "-1"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-control", "no-cache"); response.setHeader("Content-Type", "text/html; charset=utf-8"); PrintWriter out = response.getWriter(); try { String charset = Constants.charset; /* String from = new String(request.getParameter("from").getBytes(charset), "utf-8"); String to = new String(request.getParameter("to").getBytes(charset), "utf-8"); String cc = new String(request.getParameter("cc").getBytes(charset), "utf-8"); String bcc = new String(request.getParameter("bcc").getBytes(charset), "utf-8"); String subject = new String(request.getParameter("subject").getBytes(charset), "utf-8"); String body= new String(request.getParameter("body").getBytes(charset), "utf-8"); */ String from = request.getParameter("from"); String to = request.getParameter("to"); String cc = request.getParameter("cc"); String bcc = request.getParameter("bcc"); String subject = request.getParameter("subject"); String body = request.getParameter("body"); // learn the global charset setting. // learn user preferences from the DB. AuthProfile auth = getAuthProfile(request); // now create a new email object. Email email = new Email(); EmailHeader header = new EmailHeader(); Address adrs[] = Utility.stringToAddressArray(from); header.setFrom(adrs); header.setTo(Utility.stringToAddressArray(to)); if (cc != null && cc.trim() != "") { header.setCc(Utility.stringToAddressArray(cc)); } if (bcc != null && bcc.trim() != "") { header.setBcc(Utility.stringToAddressArray(bcc)); } header.setSubject(subject); header.setDate(new Date()); email.setBaseHeader(header); ArrayList parts = new ArrayList(); EmailPart bodyPart = new EmailPart(); bodyPart.setContentType("text/html; charset=" + charset); bodyPart.setContent(body); parts.add(0, bodyPart); // attach some files... ArrayList attachments = (ArrayList) request.getSession().getAttribute("attachments"); if (attachments != null) { List newLst = new ArrayList(); EmailPart tmp = null; for (int i = 0; i < attachments.size(); i++) { try { tmp = (EmailPart) attachments.get(i); String disp = tmp.getDisposition(); File f = new File(disp); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); byte data[] = new byte[(int) f.length()]; bis.read(data); MimeBodyPart bp = new MimeBodyPart(); DataSource ds = new ByteArrayDataSource(data, tmp.getContentType(), tmp.getFilename()); bp.setDataHandler(new DataHandler(ds)); bp.setDisposition("attachment; filename=\"" + tmp.getFilename() + "\""); tmp.setDisposition(bp.getDisposition()); bp.setFileName(tmp.getFilename()); tmp.setDataSource(ds); tmp.setContent(bp.getContent()); newLst.add(tmp); bis.close(); } catch (Exception e) { e.printStackTrace(); } } parts.addAll(newLst); } email.setParts(parts); // it is time to send the email object message Smtp smtp = new Smtp(getConnectionProfile(request), getAuthProfile(request)); HashMap sendRes = smtp.send(email, true); MimeMessage msg = (MimeMessage) sendRes.get("msg"); saveDraft(auth, msg, request); out.print("ok"); } catch (Exception e) { out.print("fail"); } }
/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = new ArrayList(); Folder fold = null; try { fold = getFolder(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fp.add("Disposition-Notification-To"); fp.add("X-Priority"); fp.add("X-MSMail-Priority"); fp.add("Sensitivity"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(org.claros.commons.utility.Utility.updateTRChars(msg.getSubject())); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown( org.claros.commons.utility.Utility.updateTRChars( Utility.addressArrToStringShort(header.getFrom()))); header.setToShown(Utility.addressArrToStringShort(header.getTo())); header.setCcShown(Utility.addressArrToStringShort(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); org.claros.commons.mail.parser.MessageParser.setHeaders(msg, header); boolean deleted = false; if (profile.getProtocol().equals(Constants.IMAP)) { Flags.Flag flags[] = msg.getFlags().getSystemFlags(); if (flags != null) { Flags.Flag flag = null; for (int m = 0; m < flags.length; m++) { flag = flags[m]; if (flag.equals(Flags.Flag.SEEN)) { header.setUnread(new Boolean(false)); } if (flag.equals(Flags.Flag.DELETED)) { deleted = true; } } } } if (header.getUnread() == null) { header.setUnread(new Boolean(true)); } // it is time to add it to the arraylist if (!deleted) { headers.add(header); } } catch (MessagingException e1) { log.error( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted.", e1); } } } catch (MessagingException e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } catch (Exception e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } return headers; }