2015-11-17 162 views
0

我能夠使用Apache POI成功創建密碼保護的.xls文件。但是,當我使用JavaMail作爲附件發送它時,我在收件人電子郵件地址中收到的文件不再受密碼保護。有誰知道爲什麼會發生這種情況?JavaMail密碼保護附件

 final String fname = "sample.xls"; 

     FileInputStream fileInput = null;  
     BufferedInputStream bufferInput = null;  
     POIFSFileSystem poiFileSystem = null;  
     FileOutputStream fileOut = null; 

     try {   
      fileInput = new FileInputStream(fname);   
      bufferInput = new BufferedInputStream(fileInput);    
      poiFileSystem = new POIFSFileSystem(bufferInput);    

      Biff8EncryptionKey.setCurrentUserPassword("secret");    
      final HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);    
      final HSSFSheet sheet = workbook.getSheetAt(0);   

      final HSSFRow row = sheet.createRow(0); 
      final Cell cell = row.createCell(0); 

      cell.setCellValue("THIS WORKS!"); 

      fileOut = new FileOutputStream(fname); 
      workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), ""); 
      workbook.write(fileOut); 
      workbook.close(); 
     } 
     catch (final Exception ex){ 
      System.out.println(ex.getMessage());  
     } 
     finally{ 
      try{ 
       bufferInput.close();  
      } 
      catch (final IOException ex){ 
       System.out.println(ex.getMessage());  
      } 
      try {    
       fileOut.close(); 
      } 
      catch (final IOException ex) { 
       System.out.println(ex.getMessage());  
      } 
     } 
     // Recipient's email ID needs to be mentioned. 
     final String to = "[email protected]"; 

     // Sender's email ID needs to be mentioned 
     final String from = "[email protected]"; 

     final String username = "example";//change accordingly 
     final String password = "example";//change accordingly 

     // Assuming you are sending email through relay.jangosmtp.net 
     final String host = "example"; 

     final Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", "example"); 

     // Get the Session object. 
     final Session session = Session.getInstance(props, new javax.mail.Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
     }); 

     try { 
      // Create a default MimeMessage object. 
      final Message message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(from)); 

      // Set To: header field of the header. 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 

      // Set Subject: header field 
      message.setSubject("Testing Subject"); 

      // Create the message part 
      BodyPart messageBodyPart = new MimeBodyPart(); 

      // Now set the actual message 
      messageBodyPart.setText("This is message body"); 

      // Create a multipar message 
      final Multipart multipart = new MimeMultipart(); 

      // Set text message part 
      multipart.addBodyPart(messageBodyPart); 

      // Part two is attachment 
      messageBodyPart = new MimeBodyPart(); 
      final String filename = "sample.xls"; 
      final DataSource source = new FileDataSource(filename); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(filename); 
      multipart.addBodyPart(messageBodyPart); 

      // Send the complete message parts 
      message.setContent(multipart); 

      // Send message 
      Transport.send(message); 

      System.out.println("Sent message successfully...."); 

     } catch (final MessagingException e) { 
      throw new RuntimeException(e); 
     } 

回答