JavaMail密码保护附件

时间:2015-11-17 19:00:19

标签: java encryption apache-poi javamail password-protection

我能够使用Apache POI成功创建受密码保护的.xls文件。但是,当我使用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 = "example@example.com";

        // Sender's email ID needs to be mentioned
        final String from = "example@example.com";

        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);
        }

1 个答案:

答案 0 :(得分:0)

我相信我没有正确地进行加密。我在这里使用了代码:http://www.quicklyjava.com/create-password-protected-excel-using-apache-poi/。它解决了这个问题。