2014-09-10 3 views
2

Google Mail (Gmail - imaps) 계정의 모든 메일을 읽고 첨부 파일을 다운로드하려고하는데 읽지 않은 메일과 첨부 파일을 하나만 가져올 수 있습니다.apache camel을 사용하여 Gmail받은 편지함에서 모든 메일 읽기

내 코드 스 니펫 게시.

// Download function 

public void download() throws Exception { 

     PollingConsumer pollingConsumer = null; 
     CamelContext context = new DefaultCamelContext(); 

     Endpoint endpoint = 
       context.getEndpoint("imaps://imap.gmail.com?username=" 
         + mailId + "&password=" + password 
         + "&delete=false&peek=false&unseen=true&consumer.delay=60000&closeFolder=false&disconnect=false"); 

     pollingConsumer = endpoint.createPollingConsumer(); 
     pollingConsumer.start(); 

     pollingConsumer.getEndpoint().createExchange(); 
     Exchange exchange = pollingConsumer.receive(); 

     log.info("exchange : " + exchange.getExchangeId()); 
     process(exchange); 

} 

// mail process function 

public void process(Exchange exchange) throws Exception { 
    Map<String, DataHandler> attachments = exchange.getIn().getAttachments(); 

    Message messageCopy = exchange.getIn().copy(); 

    if (messageCopy.getAttachments().size() > 0) { 
     for (Map.Entry<String, DataHandler> entry : messageCopy.getAttachments().entrySet()) { 
      DataHandler dHandler = entry.getValue(); 
      // get the file name 
      String filename = dHandler.getName(); 

      // get the content and convert it to byte[] 
      byte[] data = 
        exchange.getContext().getTypeConverter().convertTo(byte[].class, dHandler.getInputStream()); 

      FileOutputStream out = new FileOutputStream(filename); 
      out.write(data); 
      out.flush(); 
      out.close(); 
      log.info("Downloaded attachment, file name : " + filename); 

     } 
    } 
} 

모든 메일 (받은 편지함, 읽지 않음)을 반복하도록 도와주세요.

답변

2

Exchange exchange = pollingConsumer.receive();을 반복 실행해야합니다.

예 :

Exchange ex = pollingConsumer.receive(); 
while (ex != null) { 
    process(ex); 
    ex = pollingConsumer.receive(); 
} 
+0

내 질문은 내 메일을 반복 할 수있는 방법입니다. 지금은 읽지 않은 메일이 1 개 있지만받은 편지함에서 읽지 않은 메일이 모두 필요합니다. –

+0

죄송합니다. 제목에 근거하여 질문을 잘못 읽었습니다. 수정 프로그램의 대답 편집. –

관련 문제