Exemplo n.º 1
0
 public boolean isMessageTooOld(Account account, Message message, String context)
     throws MessagingException {
   if (message.getSentDate() == null) {
     log.warn(
         String.format("we have a message with no sent date for %s, allowing message", context));
     return false;
   } else if (account.getRegister_time() == null) {
     log.warn(
         String.format(
             "we are process an account with no register time. this behavior is not understood yet %s, we will accept this message",
             context));
     return false;
   } else {
     boolean messageTooOld =
         (System.currentTimeMillis() - message.getSentDate().getTime())
             > 1000l * 60 * 60 * 24 * emailDaysCutoff;
     if (messageTooOld) {
       log.warn(
           String.format(
               "msgNum=%d, message is too old, sentDate=%s, discarding, for %s",
               message.getMessageNumber(), message.getSentDate(), context));
     }
     return messageTooOld;
   }
 }
Exemplo n.º 2
0
 public Date getMessageDate(Message message) throws MessagingException {
   Date messageDate = message.getReceivedDate();
   if (message.getReceivedDate() == null) {
     if (message.getSentDate() != null) {
       messageDate = message.getSentDate();
     }
   }
   return messageDate;
 }
    /**
     * Handel multipart messages recursive until we find the first text/html message. Or text/plain
     * if html is not available.
     *
     * @param multipart the multipart portion
     * @param message the message
     * @param builder the email value builder
     * @throws MessagingException
     * @throws IOException
     */
    private void handleMultipart(
        Multipart multipart, Message message, ValueBuilder<EmailValue> builder)
        throws MessagingException, IOException {
      String body = "";

      String contentType = cleanContentType(multipart.getContentType());

      for (int i = 0, n = multipart.getCount(); i < n; i++) {
        BodyPart part = multipart.getBodyPart(i);

        String disposition = part.getDisposition();

        if ((disposition != null)
            && ((disposition.equalsIgnoreCase(Part.ATTACHMENT)
                || (disposition.equalsIgnoreCase(Part.INLINE))))) {
          builder
              .prototype()
              .attachments()
              .get()
              .add(createAttachedFileValue(message.getSentDate(), part));

        } else {
          if (part.isMimeType(Translator.PLAIN)) {
            // if contents is multipart mixed concatenate text plain parts
            if ("multipart/mixed".equalsIgnoreCase(contentType)) {
              body += (String) part.getContent() + "\n\r";
            } else {
              body = (String) part.getContent();
            }
            builder.prototype().content().set(body);
            builder.prototype().contentType().set(Translator.PLAIN);

          } else if (part.isMimeType(Translator.HTML)) {
            body = (String) part.getContent();
            builder.prototype().contentHtml().set(body);
            builder.prototype().contentType().set(Translator.HTML);

          } else if (part.isMimeType("image/*")) {
            builder
                .prototype()
                .attachments()
                .get()
                .add(createAttachedFileValue(message.getSentDate(), part));

          } else if (part.getContent() instanceof Multipart) {
            handleMultipart((Multipart) part.getContent(), message, builder);
          }
        }
      }
      // if contentHtml is not empty set the content type to text/html
      if (!Strings.empty(builder.prototype().contentHtml().get())) {
        builder.prototype().content().set(builder.prototype().contentHtml().get());
        builder.prototype().contentType().set(Translator.HTML);
      }
    }
Exemplo n.º 4
0
  private void setTextArea() {

    String date = "";
    String from = "";
    String to = "";

    to = removeFirstSpace(messageHandler.getToText());

    try {

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/YYYY hh:mm a");
      date = simpleDateFormat.format(message.getSentDate());
      from = getFrom();
    } catch (MessagingException e) {
      e.printStackTrace();
    }

    referenceTextArea = textArea;

    textArea.setText(
        "\n\n\n"
            + ">Date: "
            + date
            + "\n"
            + ">From: "
            + from
            + "\n>To: "
            + to
            + "\n>"
            + messageHandler.getText());

    textArea.setCaretPosition(0);
  }
 public CustomProperties getMessageProperties(Message message, String prefix, CustomProperties p)
     throws MessagingException, IOException {
   CustomProperties envVars = new CustomProperties();
   String msgSubject = stringify(message.getSubject());
   envVars.put(prefix + "subject", msgSubject);
   envVars.put(prefix + "from", stringify(message.getFrom()));
   envVars.put(prefix + "replyTo", stringify(message.getReplyTo()));
   envVars.put(prefix + "flags", stringify(message.getFlags()));
   envVars.put(prefix + "folder", stringify(message.getFolder()));
   envVars.put(prefix + "messageNumber", stringify(message.getMessageNumber()));
   envVars.put(prefix + "receivedDate", stringify(message.getReceivedDate()));
   envVars.put(prefix + "sentDate", stringify(message.getSentDate()));
   envVars.put(prefix + "headers", stringify(message.getAllHeaders()));
   envVars.put(prefix + "content", stringify(message));
   envVars.put(prefix + "contentType", stringify(message.getContentType()));
   envVars.put(prefix + "recipients", stringify(message.getAllRecipients()));
   // add parameters from email content
   final CustomProperties properties = CustomProperties.read(getText(message));
   envVars.putAll(properties);
   //            envVars.put(prefix + "emailParams", stringify(properties.getMap(), "=", "&"));
   // add "jobTrigger"
   if (p.has(subjectContains)) {
     String subject = p.get(subjectContains);
     // normalise strings, find index, then get text after it
     int idx = msgSubject.toLowerCase().indexOf(subject.toLowerCase());
     int beginIndex = idx + subject.length();
     if (idx > -1 && beginIndex < msgSubject.length()) {
       envVars.put(prefix + "jobTrigger", msgSubject.substring(beginIndex).trim());
     }
   }
   return envVars;
 }
  /**
   * metoda preuzmiPoruke preuzima poruke iz odabrane mape i puni listu poruka
   *
   * @throws MessagingException
   * @throws IOException
   */
  private void preuzmiPoruke() throws MessagingException, IOException {
    Message[] messages;

    // Open the INBOX folder
    folder = store.getFolder(this.odabranaMapa);
    folder.open(Folder.READ_ONLY);

    messages = folder.getMessages();

    this.poruke = new ArrayList<Poruka>();

    for (int i = 0; i < messages.length; ++i) {
      Message m = messages[i];
      Poruka p =
          new Poruka(
              m.getHeader("Message-ID")[0],
              m.getSentDate(),
              m.getFrom()[0].toString(),
              m.getSubject(),
              m.getContentType(),
              m.getSize(),
              0,
              m.getFlags(),
              null,
              true,
              true,
              m.getContent().toString());
      // TODO potraziti broj privitaka, sad je hardkodirano da ih je 0

      this.poruke.add(p);
    }
  }
Exemplo n.º 7
0
  /**
   * Deliveres notification to the user that an email has been dropped. Typically due to size
   * restriction
   *
   * @param account
   * @param messageNumber
   * @param message
   * @throws Exception
   */
  public void saveMessageDroppedNotification(
      Account account, int messageNumber, Message message, int reportedSize) throws Exception {
    MessageParser parser = new MessageParser();

    Email email = new Email();
    email.setSubject(
        "Your email"); // ReceiverUtilsTools.subSubject(parser.parseMsgSubject(message),
                       // subjectSize));
    email.setFrom(parser.parseMsgAddress(message, "FROM", false));
    email.setTo(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "TO", true)));
    email.setCc(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "CC", true)));
    email.setBcc(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "BCC", true)));
    email.setMaildate(ReceiverUtilsTools.dateToStr(message.getSentDate()));
    email.setStatus("0");
    email.setUserId(account.getUser_id());
    email.setMessage_type("EMAIL");

    Body body = new Body();

    String droppedMessage =
        getEmailDroppedMessage(account, getMessageDate(message), reportedSize, email.getFrom());

    body.setData(droppedMessage.getBytes());
    email.setBodySize(droppedMessage.length());

    int saveStatus = DALDominator.newSaveMail(account, email, body);

    if (log.isDebugEnabled())
      log.debug(
          String.format(
              "[%s] msgNum=%d, saving completed for dropped message with status %s",
              account.getName(), messageNumber, saveStatus));
  }
Exemplo n.º 8
0
 /** Returns the date the message was sent (or received if the sent date is null. */
 public String getDate() throws MessagingException {
   Date date;
   SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
   if ((date = message.getSentDate()) != null) return (df.format(date));
   else if ((date = message.getReceivedDate()) != null) return (df.format(date));
   else return "";
 }
  /**
   * 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;
  }
Exemplo n.º 10
0
  public static void dumpEnvelope(Message m) throws Exception {
    pr("This is the message envelope");
    pr("---------------------------");
    Address[] a;
    // FROM
    if ((a = m.getFrom()) != null) {
      for (int j = 0; j < a.length; j++) pr("FROM: " + a[j].toString());
    }

    // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
      for (int j = 0; j < a.length; j++) pr("TO: " + a[j].toString());
    }

    // SUBJECT
    pr("SUBJECT: " + m.getSubject());

    // DATE
    Date d = m.getSentDate();
    pr("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));

    // FLAGS
    Flags flags = m.getFlags();
    StringBuffer sb = new StringBuffer();
    Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

    boolean first = true;
    for (int i = 0; i < sf.length; i++) {
      String s;
      Flags.Flag f = sf[i];
      if (f == Flags.Flag.ANSWERED) s = "\\Answered";
      else if (f == Flags.Flag.DELETED) s = "\\Deleted";
      else if (f == Flags.Flag.DRAFT) s = "\\Draft";
      else if (f == Flags.Flag.FLAGGED) s = "\\Flagged";
      else if (f == Flags.Flag.RECENT) s = "\\Recent";
      else if (f == Flags.Flag.SEEN) s = "\\Seen";
      else continue; // skip it
      if (first) first = false;
      else sb.append(' ');
      sb.append(s);
    }

    String[] uf = flags.getUserFlags(); // get the user flag strings
    for (int i = 0; i < uf.length; i++) {
      if (first) first = false;
      else sb.append(' ');
      sb.append(uf[i]);
    }
    pr("FLAGS: " + sb.toString());

    // X-MAILER
    String[] hdrs = m.getHeader("X-Mailer");
    if (hdrs != null) pr("X-Mailer: " + hdrs[0]);
    else pr("X-Mailer NOT available");
  }
Exemplo n.º 11
0
  public static Date getDateFromMessage(Message message, Connection con) {

    Date result = null;

    try {
      if (hasHeader("Date", message)) {
        Date msg_date = message.getSentDate();
        // If Java Mail could not parse the date ...
        if (msg_date == null) {
          // see if the postgresql server can infer the date ...
          String datestr = message.getHeader("Date")[0];
          Main.getLogger()
              .debug(2, "Invalid Message date supplied - letting Postgresql guess: " + datestr);
          try {
            Statement st = con.createStatement();
            ResultSet r = st.executeQuery("SELECT TIMESTAMP WITH TIME ZONE '" + datestr + "'");
            if (r.next()) result = new Date(r.getTimestamp(1).getTime());
            r.close();
            st.close();
          } catch (SQLException e) {
            Main.getLogger()
                .debug(
                    2,
                    "Error asking PostgreSQL for the date - using default date. " + e.getMessage());
          }

        } else result = msg_date;
      }
      if (result == null && hasHeader("Received", message)) {
        Main.getLogger().debug(2, "No Message date supplied - trying to infer");
        String[] prtemp = message.getHeader("Received")[0].split(";");
        String datestr = (prtemp.length > 0 ? prtemp[prtemp.length - 1] : "");
        try {
          Statement st = con.createStatement();
          ResultSet r = st.executeQuery("SELECT TIMESTAMP WITH TIME ZONE '" + datestr + "'");
          if (r.next()) result = new Date(r.getTimestamp(1).getTime());
        } catch (SQLException e) {
          Main.getLogger()
              .debug(
                  2,
                  "Error asking PostgreSQL for the date - using default date. " + e.getMessage());
        }
      }
    } catch (MessagingException e) {
      Main.getLogger().error("Error while extracting the date", e);
    }

    // If nothing worked set default date
    if (result == null) {
      result = new Date(0);
    }

    return result;
  }
 public MessagesWrapper print() throws MessagingException {
   logger.info("Found message(s) : " + messages.size());
   for (Message message : messages) {
     logger.info(">>>>>>");
     logger.info("Date    : " + message.getSentDate());
     logger.info("From    : " + (message.getFrom().length > 0 ? message.getFrom()[0] : null));
     logger.info("Subject : " + message.getSubject());
     logger.info("<<<<<<");
   }
   return this;
 }
Exemplo n.º 13
0
 protected Message getFullyLoad(Message message, Folder folder, String context)
     throws MessagingException {
   try {
     ((GoogleVoiceFolder) folder).loadFully((GoogleVoiceMessage) message);
   } catch (Exception e) {
     throw new MessagingException("Exception getting googlevoice message body " + e.getMessage());
   }
   Message wrappedMessage = new MimeMessage((MimeMessage) message);
   wrappedMessage.setSentDate(message.getSentDate());
   return wrappedMessage;
 }
Exemplo n.º 14
0
 public static DateAndTime getSentTime(Message msg) {
   Date date;
   try {
     date = msg.getSentDate();
   } catch (MessagingException ex) {
     throw new RuntimeException(ex);
   }
   if (date == null) return null;
   DateAndTime result = new DateAndTime(date);
   if (result.isFuture()) result = DateAndTime.now();
   return result;
 }
Exemplo n.º 15
0
  /**
   * checks message sent date againsts a predefined email days cutoff
   *
   * @param message
   * @param migratedAccount
   * @return
   * @throws javax.mail.MessagingException
   */
  public boolean isMessageTooOld(Account account, Message message, String context)
      throws MessagingException {

    if (message.getSentDate() == null) {
      log.warn(
          String.format("we have a message with no sent date for %s, allowing message", context));
      return false;
    }

    // This should handle the first login case, we want to take up to x days worth of old emails and
    // not be rejected
    // by the Too Old check
    if (account.getLast_received_date() == null) {
      log.warn(
          String.format(
              "we are process an account with no register time. this behavior is not understood yet %s, we will accept this message",
              context));
      return (System.currentTimeMillis() - message.getSentDate().getTime())
          > 1000l * 60 * 60 * 24 * emailDaysCutoff;
    }

    return isOlder(message.getSentDate(), account.getLast_received_date());
  }
Exemplo n.º 16
0
  /**
   * Receive Email from POPServer. Use POP3 protocal by default. Thus, call this method, you need to
   * provide a pop3 mail server address.
   *
   * @param emailAddress The email account in the POPServer.
   * @param password The password of email address.
   */
  public static void receiveEmail(String host, String username, String password) {
    // param check. If param is null, use the default configured value.
    if (host == null) {
      host = POP3Server;
    }
    if (username == null) {
      username = POP3Username;
    }
    if (password == null) {
      password = POP3Password;
    }
    Properties props = System.getProperties();
    // MailAuthenticator authenticator = new MailAuthenticator(username, password);
    try {
      Session session = Session.getDefaultInstance(props, null);
      // Store store = session.getStore("imap");
      Store store = session.getStore("pop3");
      // Connect POPServer
      store.connect(host, username, password);
      Folder inbox = store.getFolder("INBOX");
      if (inbox == null) {
        throw new RuntimeException("No inbox existed.");
      }
      // Open the INBOX with READ_ONLY mode and start to read all emails.
      inbox.open(Folder.READ_ONLY);
      System.out.println("TOTAL EMAIL:" + inbox.getMessageCount());
      Message[] messages = inbox.getMessages();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      for (int i = 0; i < messages.length; i++) {
        Message msg = messages[i];
        String from = InternetAddress.toString(msg.getFrom());
        String replyTo = InternetAddress.toString(msg.getReplyTo());
        String to = InternetAddress.toString(msg.getRecipients(Message.RecipientType.TO));
        String subject = msg.getSubject();
        Date sent = msg.getSentDate();
        Date ress = msg.getReceivedDate();
        String type = msg.getContentType();
        System.out.println((i + 1) + ".---------------------------------------------");
        System.out.println("From:" + mimeDecodeString(from));
        System.out.println("Reply To:" + mimeDecodeString(replyTo));
        System.out.println("To:" + mimeDecodeString(to));
        System.out.println("Subject:" + mimeDecodeString(subject));
        System.out.println("Content-type:" + type);
        if (sent != null) {
          System.out.println("Sent Date:" + sdf.format(sent));
        }
        if (ress != null) {
          System.out.println("Receive Date:" + sdf.format(ress));
        }
        //                //Get message headers.
        //                @SuppressWarnings("rawtypes")
        //                Enumeration headers = msg.getAllHeaders();
        //                while (headers.hasMoreElements()) {
        //                    Header h = (Header) headers.nextElement();
        //                    String name = h.getName();
        //                    String val = h.getValue();
        //                    System.out.println(name + ": " + val);
        //                }

        //                //get the email content.
        //                Object content = msg.getContent();
        //                System.out.println(content);
        //                //print content
        //                Reader reader = new InputStreamReader(
        //                        messages[i].getInputStream());
        //                int a = 0;
        //                while ((a = reader.read()) != -1) {
        //                    System.out.print((char) a);
        //                }
      }
      // close connection. param false represents do not delete messaegs on server.
      inbox.close(false);
      store.close();
      //        } catch(IOException e) {
      //            LOGGER.error("IOException caught while printing the email content", e);
    } catch (MessagingException e) {
      LOGGER.error("MessagingException caught when use message object", e);
    }
  }
Exemplo n.º 17
0
  /**
   * Creates a Mail
   *
   * @param inboxHelper
   * @param message
   * @param mUId
   * @return
   * @throws FolderClosedException
   * @throws MessagingException
   * @throws IOException
   */
  public static RawMail createMail(Message message) throws FolderClosedException {
    try {
      // fill the message objects with messagedata
      String[] fromInfo = MailUtils.getFromInfo(message.getFrom());

      if (fromInfo[0] == null || fromInfo[0].isEmpty()) {
        fromInfo = MailUtils.getFromInfo(message.getReplyTo());
      }
      if (fromInfo[0] == null || fromInfo[0].isEmpty()) {
        String[] returnPath = message.getHeader("Return-Path");
        if (returnPath.length > 0) {
          fromInfo[0] = returnPath[0].trim();
          if (fromInfo[0].charAt(0) == '<') fromInfo[0].substring(1);
          if (fromInfo[0].charAt(fromInfo[0].length() - 1) == '>')
            fromInfo[0].substring(0, fromInfo[0].length() - 2);
        }
      }

      String mFromAddress = (fromInfo[0] != null) ? fromInfo[0] : "";
      RawMail mail = new RawMail();
      // Note: Mail ids are generated based on contents - do not set an id

      String mSubject = message.getSubject();
      mail.setSubject(mSubject);
      mail.setFrom(new EmailAddress(mFromAddress, fromInfo[1]));
      mail.setCreated(new Date());

      // Prevent evil mail servers from sending from the future
      Date now = new Date();
      Date sentDate = message.getSentDate();
      Date recDate = message.getReceivedDate();

      if (sentDate == null || now.before(sentDate)) sentDate = new Date();
      if (recDate == null || now.before(recDate)) recDate = new Date();

      mail.setSentDate(sentDate);
      mail.setRecievedDate(recDate);

      mail.setServerId(String.valueOf(message.getMessageNumber()));

      try {
        mail.setBody(MailUtils.getBody(message));
      } catch (Throwable ex) {
        System.out.println("Failed when reading mail body");
      }

      // Headers
      Enumeration<?> allHeaders = message.getAllHeaders();
      HashMap<String, String> mailHeaders = new HashMap<String, String>();
      while (allHeaders.hasMoreElements()) {
        Header header = (Header) allHeaders.nextElement();
        mailHeaders.put(header.getName(), header.getValue());
      }
      mail.setHeaders(mailHeaders);

      try {
        mail.setRecipients(MailUtils.getRecipients(Message.RecipientType.TO, message));
      } catch (Throwable ex) {
        System.out.println("Throwable during setRecipients");
      }
      try {
        mail.setRecipients(MailUtils.getRecipients(Message.RecipientType.CC, message));
      } catch (Throwable ex) {
        System.out.println("Throwable during setRecipients");
      }
      try {
        mail.setRecipients(MailUtils.getRecipients(Message.RecipientType.BCC, message));
      } catch (Throwable ex) {
        System.out.println("Throwable during setRecipients");
      }
      return mail;
    } catch (FolderClosedException ex) {
      throw ex;
    } catch (Throwable ex) {
      System.out.println("Could not read mail");
    }
    return null;
  }
Exemplo n.º 18
0
 private Date getDateToDetermineOrder(Message message) throws MessagingException {
   return message instanceof IMAPMessage ? message.getReceivedDate() : message.getSentDate();
 }
Exemplo n.º 19
0
  public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message) {
      Message m = (Message) p;
      Address[] a;
      // FROM
      if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++) System.out.println("FROM: " + a[j].toString());
      }

      // TO
      if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++) System.out.println("TO: " + a[j].toString());
      }

      // SUBJECT
      System.out.println("SUBJECT: " + m.getSubject());

      // DATE
      Date d = m.getSentDate();
      System.out.println("SendDate: " + (d != null ? d.toLocaleString() : "UNKNOWN"));

      // FLAGS:
      Flags flags = m.getFlags();
      StringBuffer sb = new StringBuffer();
      Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

      boolean first = true;
      for (int i = 0; i < sf.length; i++) {
        String s;
        Flags.Flag f = sf[i];
        if (f == Flags.Flag.ANSWERED) s = "\\Answered";
        else if (f == Flags.Flag.DELETED) s = "\\Deleted";
        else if (f == Flags.Flag.DRAFT) s = "\\Draft";
        else if (f == Flags.Flag.FLAGGED) s = "\\Flagged";
        else if (f == Flags.Flag.RECENT) s = "\\Recent";
        else if (f == Flags.Flag.SEEN) s = "\\Seen";
        else continue; // skip it
        if (first) first = false;
        else sb.append(' ');
        sb.append(s);
      }

      String[] uf = flags.getUserFlags(); // get the user flag strings
      for (int i = 0; i < uf.length; i++) {
        if (first) first = false;
        else sb.append(' ');
        sb.append(uf[i]);
      }
      System.out.println("FLAGS = " + sb.toString());
    }

    System.out.println("CONTENT-TYPE: " + p.getContentType());

    /* Dump input stream
    InputStream is = ((MimeMessage)m).getInputStream();
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c);
    */

    Object o = p.getContent();
    if (o instanceof String) {
      System.out.println("This is a String");
      System.out.println((String) o);
    } else if (o instanceof Multipart) {
      System.out.println("This is a Multipart");
      Multipart mp = (Multipart) o;
      int count = mp.getCount();
      for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i));
    } else if (o instanceof InputStream) {
      System.out.println("This is just an input stream");
      InputStream is = (InputStream) o;
      int c;
      while ((c = is.read()) != -1) System.out.write(c);
    }
  }
Exemplo n.º 20
0
  /**
   * Aggregates the e-mail message by reading it and turning it either into a page or a file upload.
   *
   * @param message the e-mail message
   * @param site the site to publish to
   * @throws MessagingException if fetching the message data fails
   * @throws IOException if writing the contents to the output stream fails
   */
  protected Page aggregate(Message message, Site site)
      throws IOException, MessagingException, IllegalArgumentException {

    ResourceURI uri = new PageURIImpl(site, UUID.randomUUID().toString());
    Page page = new PageImpl(uri);
    Language language = site.getDefaultLanguage();

    // Extract title and subject. Without these two, creating a page is not
    // feasible, therefore both messages throw an IllegalArgumentException if
    // the fields are not present.
    String title = getSubject(message);
    String author = getAuthor(message);

    // Collect default settings
    PageTemplate template = site.getDefaultTemplate();
    if (template == null)
      throw new IllegalStateException("Missing default template in site '" + site + "'");
    String stage = template.getStage();
    if (StringUtils.isBlank(stage))
      throw new IllegalStateException(
          "Missing stage definition in template '" + template.getIdentifier() + "'");

    // Standard fields
    page.setTitle(title, language);
    page.setTemplate(template.getIdentifier());
    page.setPublished(new UserImpl(site.getAdministrator()), message.getReceivedDate(), null);

    // TODO: Translate e-mail "from" into site user and throw if no such
    // user can be found
    page.setCreated(site.getAdministrator(), message.getSentDate());

    // Start looking at the message body
    String contentType = message.getContentType();
    if (StringUtils.isBlank(contentType))
      throw new IllegalArgumentException("Message content type is unspecified");

    // Text body
    if (contentType.startsWith("text/plain")) {
      // TODO: Evaluate charset
      String body = null;
      if (message.getContent() instanceof String) body = (String) message.getContent();
      else if (message.getContent() instanceof InputStream)
        body = IOUtils.toString((InputStream) message.getContent());
      else throw new IllegalArgumentException("Message body is of unknown type");
      return handleTextPlain(body, page, language);
    }

    // HTML body
    if (contentType.startsWith("text/html")) {
      // TODO: Evaluate charset
      return handleTextHtml((String) message.getContent(), page, null);
    }

    // Multipart body
    else if ("mime/multipart".equalsIgnoreCase(contentType)) {
      Multipart mp = (Multipart) message.getContent();
      for (int i = 0, n = mp.getCount(); i < n; i++) {
        Part part = mp.getBodyPart(i);
        String disposition = part.getDisposition();
        if (disposition == null) {
          MimeBodyPart mbp = (MimeBodyPart) part;
          if (mbp.isMimeType("text/plain")) {
            return handleTextPlain((String) mbp.getContent(), page, null);
          } else {
            // TODO: Implement special non-attachment cases here of
            // image/gif, text/html, ...
            throw new UnsupportedOperationException(
                "Multipart message bodies of type '"
                    + mbp.getContentType()
                    + "' are not yet supported");
          }
        } else if (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE)) {
          logger.info("Skipping message attachment " + part.getFileName());
          // saveFile(part.getFileName(), part.getInputStream());
        }
      }

      throw new IllegalArgumentException(
          "Multipart message did not contain any recognizable content");
    }

    // ?
    else {
      throw new IllegalArgumentException("Message body is of unknown type '" + contentType + "'");
    }
  }
Exemplo n.º 21
0
  /**
   * 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;
  }
Exemplo n.º 22
0
 /** Method for checking if the message has a sent date field. */
 public boolean hasSentDate() throws MessagingException {
   return (message.getSentDate() != null);
 }
Exemplo n.º 23
0
 /** Returns the sent date field. */
 public String getSentDate() throws MessagingException {
   if (hasSentDate()) return (message.getSentDate().toString());
   else return "";
 }
Exemplo n.º 24
0
  public boolean processMessage(
      Account account,
      Message message,
      int messageNumber,
      String messageId,
      Folder folder,
      Set<String> storeMessageIds,
      String storeBucket,
      String context,
      StopWatch watch,
      FolderHelper folderHelper)
      throws Exception {
    boolean result = false;

    long start = System.nanoTime();

    try {
      StopWatchUtils.newTask(
          watch, String.format("msgNum=%s, storeCheck", messageNumber), context, log);

      // Log.debug(log4j, "msgNum=%d, checking if messageId=%s is known for %s", messageNumber,
      // messageId, context);

      // Log.debug(log4j, "msgNum=%d, checking if messageId=%s is too old or too big or sent for
      // %s", messageNumber, messageId, context);

      StopWatchUtils.newTask(
          watch, String.format("msgNum=%s, tooOld-tooBig-check", messageNumber), context, log);

      if (isMessageTooOld(account, message, context)) {
        log.warn(
            String.format(
                "msgNum=%d, messageId=%s, message is too old, sentDate=%s, discarding, for %s",
                messageNumber, messageId, message.getSentDate(), context));

        // this message is too old and needs to be ignored
      } else {
        message = getFullyLoad(message, folder, context);
        int messageSize =
            (folder instanceof POP3Folder)
                ? ((POP3Folder) folder).getMessageSize(messageNumber)
                : getMessageSize(message, account);

        if (messageSize > maximumMessageSize) {
          log.warn(
              String.format(
                  "msgNum=%d, messageId=%s, message is too big %d bytes, discarding for %s",
                  messageNumber, messageId, messageSize, context));
          saveMessageDroppedNotification(account, messageNumber, message, messageSize);
        } else {
          if (log.isDebugEnabled())
            log.debug(
                String.format(
                    "msgNum=%d, messageId=%s, message is new for %s",
                    messageNumber, messageId, context));

          try {
            new EmailSaveProcess(account, watch, context)
                .process(message, messageNumber, messageId);
          } catch (Throwable e) {
            log.fatal(
                String.format(
                    "msgNum=%d, messageId=%s, subject=%s message saving failed for %s",
                    messageNumber, messageId, message.getSubject(), context),
                e);

            StopWatchUtils.newTask(
                watch, String.format("msgNum=%s, removeFromStore", messageNumber), context, log);
          }

          result = true;
        }
      }
    } finally {
      if (log.isDebugEnabled())
        log.debug(
            String.format(
                "msgNum=%d, checked if messageId=%s is known in %dms for %s",
                messageNumber, messageId, (System.nanoTime() - start) / 1000000, context));
    }

    return result;
  }