2017-01-04 1 views
-2

Java 코드에서 전자 메일에 회신하려고하는데 실제 회신을 보내면 보낸 날짜가 잘못되었습니다. 나는 Exchange 서비스가 UTC 시간을 고려하고 있다고 생각합니다.EWS Java - 전자 메일에 대한 회신이 잘못된 날짜입니다.

실제 날짜 Sent- 화 2017년 1월 3일 오후 3시 58분

받은 날짜 - 일 화요일, 1 월 3 일 나는를 설정하는 방법을 모르는 2017 오후 8시 58분 51초

동부 표준시 고려해야 할 교환 서비스 시간.

내가

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(); 
} 
관련 문제