Rahul U
Rahul U

Reputation: 11

Password protected excel as mail attachment

I have created a jar for sending mail using java. I have to attach an excel file with mail. Am using HSSF for creating worksheet. But I need to encrypt the attachment with password. I succeeded. But when I open the attachment directly from mail through Outlook it's not asking password. and when I copy to any folder and then if I try to open , it works properly. Can anyone, please help?

public static void main(final String... args) throws Exception {     

        String fname = "D:\\Mail\\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");            
            HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);            
            HSSFSheet sheet = workbook.getSheetAt(0);           

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

            cell.setCellValue("THIS WORKS!"); 

            fileOut = new FileOutputStream(fname);
            workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");
            workbook.write(fileOut);

            File file = new File("D:\\Mail\\Sample.xls");

            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            try {
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    bos.write(buf, 0, readNum); 
                    System.out.println("read " + readNum + " bytes,");
                }
            } catch (IOException ex) {
            }
            byte[] bytes = bos.toByteArray();   

// Code for sending mail         

        } catch (Exception ex) {

            System.out.println(ex.getMessage());      

        } finally {         

              try {            

                  bufferInput.close();     

              } catch (IOException ex) {

                  System.out.println(ex.getMessage());     

              }    

              try {            

                  fileOut.close();     

              } catch (IOException ex) {

                  System.out.println(ex.getMessage());     

              } 
        }       

    }

Upvotes: 0

Views: 1156

Answers (1)

walen
walen

Reputation: 7273

Method HSSFWorkbook.writeProtectWorkbook(...) is only meant to protect the workbook from being written to / modified.
If you try to open it in read+write mode (default in Windows folders), it will ask you for the password.
But if you open it in read-only mode, which is what Outlook does with attachments, it will let you view the contents since you can't write over them and thus you don't need the write password.
That's the reason you can view (but not edit) it in Outlook but not when you open it from a folder.

I don't know if the latest version of Apache POI supports full password protection for an HSSFWorkBook (a quick Google search says it doesn't, but who knows).

If it doesn't, you can work around this problem by making a password-protected ZIP file with the excel in it, and attaching the ZIP file instead.

Upvotes: 1

Related Questions