EWS Java - 回复电子邮件的日期不正确

时间:2017-01-04 23:09:23

标签: java exchangewebservices ewsjavaapi microsoft-exchange

我正在尝试回复来自java代码的电子邮件,当我收到回复时,发送日期在实际电子邮件中是不正确的。我认为Exchange服务正在考虑UTC时间。

实际发送日期 - 2017年1月3日下午3:58

收到日期 - 2017年1月3日星期二下午8:58:51

我不知道如何设置Exchange服务时间以考虑东部时间。

我可以使用

获取服务器时区
Collection<TimeZoneDefinition> response = service.getServerTimeZones();

但是如何将服务设置为仅使用东部时间。?

这是我的回复代码。

 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 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,这就是我最终做的事情。希望它可以帮到某人。

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();
}