2015-08-21 46 views
-4

我是java和PDFBox的新手。 我已經使用PDFBox分割PDF並將其保存在一個目標文件夾中。文件名稱如下 - > D0000025667-T04292.pdf,D0000025668-T02119.pdf,D0000025670-T01125.pdf等。 我已經連接到MS Access數據庫和表值如下所示:如何使用java搜索PDF文件並將其附加到電子郵件

**Dealer Code  Email** 
T04292   [email protected] 
T04292   [email protected] 
T02119   [email protected] 
T01125   [email protected] 
RS0009   [email protected] 
RS0001   [email protected] 
C01020   [email protected] 

我知道如何在Java附件發送郵件。我的要求是,我需要從表格中獲取經銷商代碼,並在經銷商代碼的幫助下在目標中搜索PDF。

最後,我需要附加併發送文件到相應的電子郵件ID。請幫我解決這個問題。

+0

**你**做了什麼?你碰到的**問題是什麼?你的問題聽起來像「我已經分配了一項任務,爲我做!」 – mkl

+0

它的nt像tht。使用PDFBox我已經分割了PDF。但新西蘭我需要搜索和附加特定的電子郵件的文件。但我試過簡單的電子郵件功能。我被搜查並附上pdf – User9999

回答

1

我不是100%確定你真正想要什麼,但我認爲這會幫助你: 此代碼訪問給定路徑中的所有文件,並將文件名中的dealerName過濾掉。比你可以與去(讀爲經銷商的電子郵件,然後發送郵件)

Path startPath = Paths.get("pathToYourDirectoryWithTheFiles"); 
    try { 
     Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() { 
      @Override 
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 

       if (Files.isRegularFile(file)) { 
        String filename = file.getFileName().toString(); 

        Pattern stringPattern = Pattern.compile("\\w*-(\\w*)\\.pdf"); 
        Matcher matcher = stringPattern.matcher(filename); 
        if (matcher.find()) { 
         String dealer = matcher.group(1); 

         System.out.println(dealer); 

         // String mail = getMailForDealer(dealer); 
         // sendMailToDealer(mail,file); 
        } 

       } 

       return FileVisitResult.CONTINUE; 
      } 
     }); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

您必須實現方法getMailForDealer(dealer)sendMailToDealer(mail,file)自己。

getMailForDealer你讀的emailadress爲的經銷商給出的(從數據庫或者也許你已經將數據讀入像Map<String,String>結構)

sendMailToDealer

你只是將該文件附加到您從getMailForDealer得到了有電子郵件地址

+0

此代碼正常工作。新的我可以列出所有的delaer代碼。我需要將該文件附加到該電子郵件,該經銷商代碼在DB中匹配.. Hw我可以執行此操作 – User9999

相關問題