-2

我正在尝试从 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
4

1 回答 1

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();
}
于 2017-01-27T17:33:41.427 回答