/**
  * Get the email received date from message header if it can not get directly.
  *
  * @param message
  * @return
  * @throws MessagingException
  * @throws ParseException
  */
 protected Date resolveReceivedDate(MimeMessage message)
     throws MessagingException, ParseException {
   if (message.getReceivedDate() != null) {
     return message.getReceivedDate();
   }
   String[] receivedHeaders = message.getHeader("Received");
   if (receivedHeaders == null) {
     return Calendar.getInstance().getTime();
   }
   SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
   Date finalDate = Calendar.getInstance().getTime();
   finalDate.setTime(0l);
   boolean found = false;
   for (String receivedHeader : receivedHeaders) {
     Pattern pattern = Pattern.compile("^[^;]+;(.+)$");
     Matcher matcher = pattern.matcher(receivedHeader);
     if (matcher.matches()) {
       String regexpMatch = matcher.group(1);
       if (regexpMatch != null) {
         regexpMatch = regexpMatch.trim();
         Date parsedDate = sdf.parse(regexpMatch);
         if (parsedDate.after(finalDate)) {
           // finding the later date mentioned in received header
           finalDate = parsedDate;
           found = true;
         }
       } else {
         LOG.error("Unable to match received date in header string: " + receivedHeader);
       }
     }
   }
   return found ? finalDate : Calendar.getInstance().getTime();
 }
 public Timestamp getReceivedDate() {
   MimeMessage message = getMessage();
   try {
     return UtilDateTime.toTimestamp(message.getReceivedDate());
   } catch (MessagingException e) {
     Debug.logError(e, module);
     return null;
   }
 }
Exemple #3
0
 public static Map<String, Object> mcaTest(DispatchContext dctx, Map<String, ?> context) {
   MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper");
   MimeMessage message = wrapper.getMessage();
   try {
     if (message.getAllRecipients() != null) {
       Debug.log("To: " + UtilMisc.toListArray(message.getAllRecipients()), module);
     }
     if (message.getFrom() != null) {
       Debug.log("From: " + UtilMisc.toListArray(message.getFrom()), module);
     }
     Debug.log("Subject: " + message.getSubject(), module);
     if (message.getSentDate() != null) {
       Debug.log("Sent: " + message.getSentDate().toString(), module);
     }
     if (message.getReceivedDate() != null) {
       Debug.log("Received: " + message.getReceivedDate().toString(), module);
     }
   } catch (Exception e) {
     Debug.logError(e, module);
   }
   return ServiceUtil.returnSuccess();
 }