(no subject)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

(no subject)

DeGuzman, Carlos

Hello,

We're trying to send an encrypted email with a pdf attachment. The situation is, the encryption works all the way through its processing and the text content part (body part 1) is visible upon receipt of the email. But the attachment seems to be missing. I found out that sending the eMail without passing it through the following statement, works just fine.

bp2 = gen.generate(bp2, SMIMEEnvelopedGenerator.RC2_CBC, "BC");

However, the size of the email message seems to contain the attachment sent at 24KB. The size of the email message without the attachment (via commenting the code block which produces the attachment part), is only 3KB.

Any help is greatly appreciated! Thanks.

Here's the code snippet:

==========================================================

  // Open the key store
  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
  ks.load(new FileInputStream(args[0]), args[1].toCharArray());
  Enumeration e = ks.aliases();
  String keyAlias = null;
  String alias = null;
  while (e.hasMoreElements()) {
   alias = (String) e.nextElement();
   if (ks.isKeyEntry(alias)) {
    keyAlias = alias;
   }
  }
  if (keyAlias == null) {
   System.err.println("can't find a private key!");
   System.exit(0);
  }
  Certificate[] chain = ks.getCertificateChain(keyAlias);

  // create the generator for creating an smime/encrypted message
  SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
  gen.addKeyTransRecipient((X509Certificate) chain[0]);

  // Generate the encrypted bodyparts

  // create the base for our message
  MimeBodyPart bp1 = new MimeBodyPart();
  bp1.setHeader("Content-Type", "text/plain");
  bp1.setContent("Hello World!!!", "text/plain; charset=\"ISO-8859-1\"");
  bp1 = gen.generate(bp1, SMIMEEnvelopedGenerator.RC2_CBC, "BC");

  // Attach the file
  MimeBodyPart bp2 = new MimeBodyPart();
  FileDataSource fileAttachment = new FileDataSource("test.pdf");
  bp2.setDataHandler(new DataHandler(fileAttachment));
  bp2.setFileName(fileAttachment.getName());
  bp2.setHeader("Content-Type", "application/pdf");
  bp2 = gen.generate(bp2, SMIMEEnvelopedGenerator.RC2_CBC, "BC");
  bp2.setContent(new DataHandler(fileAttachment), "application/pdf");
  
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(bp1);
  multipart.addBodyPart(bp2);

  // Get a Session object and create the mail message
  Properties props = System.getProperties();
  props.setProperty("mail.smtp.host", "mail.sbs.de");
  Session session = Session.getDefaultInstance(props, null);
  Address fromUser = new InternetAddress("<fromAddress>");
  Address toUser = new InternetAddress("<toAddress>
");
  MimeMessage theEMail = new MimeMessage(session);
  theEMail.setHeader("Content-Type", "multipart/mixed");
  theEMail.setFrom(fromUser);
  theEMail.setRecipient(Message.RecipientType.TO, toUser);
  theEMail.setSubject("Example Encrypted Message");
  theEMail.setContent(multipart);
  theEMail.saveChanges();
  Transport.send(theEMail);

=======================================================

Regards,

Carl