通过java本身保护作为密码保护的附件

时间:2015-05-17 07:18:01

标签: java pdf javamail

我开发了一个java邮件api程序,它将发送邮件并附加pdf文件,所以最后发送一个邮件,其中附加了pdf文件。

现在可以请您建议我想通过我的java程序本身将该pdf文件作为密码保护,例如我想修改我的下面的程序,例如打开pdf文件,创建密码1234以及发送邮件时客户端应该打开pdf文件但是在打开之前他应该在pdf文件的弹出框中输入1234来查看它,请你告诉我如何在java程序本身中实现这一点。先谢谢以下是我的java程序

public class BrokMailTest {

    public static void main(String[] args) {

        String mailSmtpHost = "77.77.77.77";
        String mailSmtpPort = "4321" ;

         String mailTo = "avdg@abc.com";
        //String mailCc = "avdg@abc.com ";
        String mailFrom = "avdg@abc.com";
        String mailSubject = "*****%%%%%%%%*********Email POC Brokerage for Rel 14.0****%%%%%%%%********";
        String mailText = "Test Mail for Brokerage POC";
        sendEmail(mailTo,  mailFrom, mailSubject, mailText, mailSmtpHost ,mailSmtpPort );
    }

    public static void sendEmail(String to,  String from, String subject, String text, String smtpHost , String mailSmtpPort) {
        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", smtpHost);
            properties.put("mailSmtpPort", mailSmtpPort);
            //obtaining the session
            Session emailSession = Session.getDefaultInstance(properties);
            Message emailMessage = new MimeMessage(emailSession);

            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //emailMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse("avdg@abc.com"));
            Address[] cc = new Address[] {
             new InternetAddress("avdg@abc.com"),
             new InternetAddress("saxenasaral@gmail.com")};
             emailMessage.addRecipients(Message.RecipientType.CC, cc);
             emailMessage.setFrom(new InternetAddress(from));
            emailMessage.setSubject(subject);

            //emailMessage.setContent(text, "text/html");

            // Create the message part
             BodyPart messageBodyPart = new MimeBodyPart();
             messageBodyPart.setContent(text, "text/html");
             messageBodyPart.setText(text);

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

            //emailMessage.setContent(emailMessage, "text/html");
            //emailMessage.setText(text);

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


          // Part two is attachment
             messageBodyPart = new MimeBodyPart();
             String filename = "c:\\SettingupRulesin outlook2003.pdf";
             DataSource source = new FileDataSource(filename);
             messageBodyPart.setDataHandler(new DataHandler(source));
             messageBodyPart.setFileName(filename);

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

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

            Transport.send(emailMessage);
        }    catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

取自教程:http://itextpdf.com/examples/iia.php?id=219

if

在声明文件字符串后添加public static byte[] USER = "password 1234".getBytes(); public static byte[] OWNER = "password 1234".getBytes(); public void encryptPdf(String filename, String filename) throws IOException, DocumentException { PdfReader reader = new PdfReader(filename); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileName)); stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA); stamper.close(); reader.close(); } 。 编辑:使用byte []对象作为密码名称。此版本的iText已弃用字符串用于加密目的。