title: Making attachement as password protected through java itself
body: <p>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 .</p>
  
  <p>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  </p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>