ndsfd ddsfd
ndsfd ddsfd

Reputation: 235

Making attachement as password protected through java itself

I have developed a java mail api program which will send the mail and it also attaches the pdf file , so finally a mail is delivered in which pdf file is attached .

now can you please advise i want to make that pdf file as password protected through my java program itself for example i want to modify my below program such as that for opening an pdf file password 1234 is created and whenever an mail is sent the client should open the pdf file but before opening he should enter 1234 in the pop up box of pdf file to see it , can you please advise how can i achieve this in java program itself please. Thanks in advance below is my java program

public class BrokMailTest {

    public static void main(String[] args) {

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

         String mailTo = "[email protected]";
        //String mailCc = "[email protected] ";
        String mailFrom = "[email protected]";
        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("[email protected]"));
            Address[] cc = new Address[] {
             new InternetAddress("[email protected]"),
             new InternetAddress("[email protected]")};
             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();
        }
    }
}

Upvotes: 1

Views: 1684

Answers (1)

TomMonTom
TomMonTom

Reputation: 45

Taken from the tutorial: http://itextpdf.com/examples/iia.php?id=219

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

Add encryptPdf(fileName, fileName); after the file string has been declared. Edit: Used byte[] objects for password names. The usage of strings have been deprecated from this version of iText for encryption purposes.

Upvotes: 1

Related Questions