2017-01-04 10 views
-2

Я пытаюсь ответить на электронную почту с кодом Java, когда я получаю ответ, дата отправки неверна в фактическом письме. Я думаю, что служба Exchange рассматривает время UTC.EWS Java - Ответ на письмо имеет неправильную дату

Фактическая дата Sent- вт 1/3/2017 3:58 вечера

Поступила дата - Вторник, 3 января 2017 8:58:51 PM

Я не знаю, как установить Обменивайте время обслуживания, чтобы рассмотреть восточное время.

Я в состоянии получить время сервера зоны с помощью

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 
+0

Какой код вы используете для отправки/получения электронной почты? Без кода, как мы можем помочь выяснить, что вы делаете неправильно? – Andreas

+0

@ Andreas- добавлен мой адрес электронной почты. – Lucky

+0

Почему -ve голосов – Lucky

ответ

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

 Смежные вопросы

  • Нет связанных вопросов^_^