2014-08-28 36 views

回答

1
  1. 您需要REST。所以 - 你不能使用org.apache.http.client庫。 (其他方式,如果您有Web服務,則可以使用wsimport創建WebService java-client。)
  2. 使用javax.mail庫打開郵箱並迭代字母。
  3. 過程字母。標記或刪除處理的字母。
  4. 添加crontab/sheduler任務來運行您的應用程序。 (至於我,這比創建一個線程應用程序更好)。不要忘記使用run-one選項來防止雙重執行。

這裏,上面寫着一些代碼示例 「的* .xls」 從郵箱中的郵件文件:

public void processEmail() throws MessagingException { 

    Store emailStore = null; 
    Folder emailFolder = null; 

    try { 
     // connecting 
     Properties properties = new Properties(); 
     properties.setProperty("mail.store.protocol", "imaps"); 
     Session emailSession = Session.getDefaultInstance(properties); 

     Main.getLog().debug("CONNECT TO : " + config.mailServer + " [" 
       + config.mailUser + "] DEBUG: "+config.debug); 

     // check mailbox 
     emailStore = emailSession.getStore("imaps"); 
     emailStore.connect(config.mailServer, config.mailUser, 
       config.mailPassword); 
     Main.getLog().debug("CONNECT DONE"); 

     emailFolder = emailStore.getFolder("INBOX"); 
     emailFolder.open(Folder.READ_WRITE); 

     Message[] messages = emailFolder.getMessages(); 
     Main.getLog().debug("RECEIVED " + messages.length + " MESSAGSES"); 

     // for each letter 
     for (Message message : messages) { 
      try { 
       Main.getLog().debug("\nPROCESS LETTER : " 
         + message.getSubject()); 
       if (message.getFlags().contains(Flags.Flag.DELETED)) {      
        continue; // don't process such letters 
       } 

       if (message.getFlags().contains(Flags.Flag.SEEN)) { 
        continue; // don't process such letters 
       } 
       // 
       Map<String, String> parseResult = new HashMap<String, String>(); 
       String auditId = ""; 

       // get file 
       if (message.getContent() instanceof Multipart) { 
        Multipart multipart = (Multipart) message.getContent(); 
        for (int i = 0; i < multipart.getCount(); i++) { 
         BodyPart bodyPart = multipart.getBodyPart(i); 
         if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart 
           .getDisposition())) { 
          continue; 
         } 
         // Process file 
         if (bodyPart.getFileName().contains(".xls")) { 
          auditId = maintainExcelFile(bodyPart.getInputStream()); 
         } 
        } 
       } else if (message.getContent() instanceof BASE64DecoderStream) { 
        // Process file 
        if (message.getFileName().contains(".xls")) { 
         auditId = maintainExcelFile(((BASE64DecoderStream) message 
           .getContent())); 
        } 
       } 
       if (!config.debug && auditId!=null && !auditId.equals("")) { 
        message.setFlag(Flags.Flag.SEEN, true); 
       } 
       if (!config.debug) { 
        sendAcceptMail(message, auditId); 
       } 
      } catch (Exception e) { 
       // Process errors 
       if (!config.debug) { 
        message.setFlag(Flags.Flag.SEEN, true); 
        sendErrorMail(message, e); 
       } 
       Main.getLog().error(e.getMessage(), e); 
       throw new Exception(e); 
      } 
     } 
    } catch (Exception e) { 
     Main.getLog().error(e.getMessage(), e); 
    } finally { 
     emailFolder.close(true); 
     emailStore.close(); 
    } 
}