EWS Java - Reply to email has incorrect date

992 views Asked by At

I'm trying to reply to email from java code, when I receive the reply, the Sent date is incorrect on the actual email. I think the Exchange service is considering UTC time.

Actual date Sent- Tue 1/3/2017 3:58 PM

Received date - Tuesday, January 3, 2017 8:58:51 PM

I don't know how to set the Exchange service time to consider Eastern time.

I'm able to get the server time zones by using

Collection<TimeZoneDefinition> response = service.getServerTimeZones();

But How to set the service to use only Eastern time.?

Here is my reply code.

 PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly,
            EmailMessageSchema.From, EmailMessageSchema.CcRecipients,
            EmailMessageSchema.Subject, EmailMessageSchema.Body,
            EmailMessageSchema.Sender, EmailMessageSchema.DateTimeReceived,
            EmailMessageSchema.Attachments);

    propertySet.setRequestedBodyType(BodyType.HTML);

    String itemId = emailMessage.getId().toString();
    EmailMessage message = EmailMessage.bind(service, new ItemId(itemId), propertySet);
    //message.getIsTimeZoneHeaderRequired(true);
    //getESTTimeZone(service);
    MessageBody errorMessage = new MessageBody();
    errorMessage.setBodyType(BodyType.HTML);
    errorMessage.setText(returnMessage);
    message.reply(errorMessage, false);   //false means do not reply all
1

There are 1 answers

0
Lucky On

I found a way around the problem, here's what I ended up doing. Hope it helps someone.

public static void reply(EmailMessage originalEmailMsg, Properties properties,
        String returnMessage, ExchangeService service)
                throws ServiceLocalException, Exception {

    String newLine = "<br>";
    String emailBody = "";

    try {
        PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties,
                EmailMessageSchema.From, EmailMessageSchema.Subject, EmailMessageSchema.Body);

        propertySet.setRequestedBodyType(BodyType.HTML);

        originalEmailMsg.load(propertySet);

        emailBody = getEmailBody(originalEmailMsg, newLine);

        ResponseMessage replyMsg = originalEmailMsg.createReply(false);

        MessageBody errorMessage = new MessageBody();
        errorMessage.setBodyType(BodyType.HTML);
        errorMessage.setText(returnMessage.concat(newLine + "<hr>" + emailBody));
        replyMsg.setBody(errorMessage);
        replyMsg.send();
        log.debug("Successfully replied to email message");

    } catch (Exception e) {
        throw e;
    }
}

    // get the original email body and concat that to the return email
    private static String getEmailBody(EmailMessage emailMessage, String newLine) throws Exception {

    StringBuffer concatenatedBody = new StringBuffer();

    String toRecipients = "";
    try {

        toRecipients = emailMessage.getToRecipients().getItems().toString();

        String sentDate = emailMessage.getDateTimeSent() != null ?  
               convertDateToEST(emailMessage.getDateTimeSent().toString()) : "";

                MessageBody ebody = emailMessage.getBody();

                String emailBody = ebody != null ? ebody.toString() : "";

                concatenatedBody.append
                ("<b>From: </b>" + emailMessage.getFrom() + newLine + 
                 "<b>Sent: </b>" + sentDate + newLine +
                 "<b>To: </b>"   + toRecipients  + newLine +
                 "<b>Subject: </b>" + emailMessage.getSubject() + newLine);

                concatenatedBody.append(emailBody);

    } catch (Exception e) {
        log.error("Error reading email body for reply email");
    }

    return concatenatedBody.toString();
}


/**
 * Convert date String coming in Fri Jan 13 10:30:46 CST 2017 format to
 * Friday, January 13, 2017 11:30 AM
 * 
 * @param dateStr
 * @return converted Date String
 * @throws Exception 
 */
public static String convertDateToEST(String dateStr) throws Exception {

    String convertedDate = "";
    TimeZone timeZone = TimeZone.getTimeZone("EST");

    try {
        //date received is in Fri Jan 13 10:30:46 CST 2017 format
        DateFormat f = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
        Date newDate = f.parse(dateStr);
        // convert it to Friday, January 13, 2017 11:30 AM format
        DateFormat out = new SimpleDateFormat("EEEE',' MMMM dd',' yyyy hh:mm a");
        out.setTimeZone(timeZone);
        convertedDate = out.format(newDate);

    } catch (Exception e) {
        throw new Exception("Exception while converting date string of " + dateStr + " : "+ e.getMessage());
    }
    return convertedDate.toString();
}