CMSException: Malformed content
Hi,
I am using bcmail-jdk15-136.jar and bcprov-jdk15-136.jar (within Eclipse WTP-1.5.3 and jdk1.5.0_11)
When I send an email with a file attachment it works fine but when I try to send an encrypted email with file attachment by adding the following statement it does not work :
MimeBodyPart encrypted = gen.generate(baseMsg, SMIMEEnvelopedGenerator.AES128_CBC, "BC");
I do not receive the attached file. The debugger shows that the encrypted object is empty.
The exception "org.bouncycastle.cms.CMSException: Malformed content" occurs at the statement
SMIMEEnveloped enveloped = new SMIMEEnveloped(baseMsg);
Many thanks for helping me.
The source code is below :
.....
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(mailContent);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fileAttachment = new FileDataSource(filePath);
DataHandler dh = new DataHandler(fileAttachment);
mbp2.setDataHandler(dh);
mbp2.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp1);
multipart.addBodyPart(mbp2);
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient((X509Certificate) cert);
//create the base for message
MimeMessage baseMsg= new MimeMessage(session);
baseMsg.setHeader("Content-Type", "multipart/mixed");
baseMsg.setFrom(new InternetAddress(senderMailAddress));
baseMsg.addRecipients(Message.RecipientType.TO, address);
baseMsg.setSubject("Example Encrypted Message");
baseMsg.setContent(multipart);
baseMsg.saveChanges();
MimeBodyPart encrypted = gen.generate(baseMsg, SMIMEEnvelopedGenerator.AES128_CBC, "BC");
Multipart finalMultipart = new MimeMultipart();
finalMultipart.addBodyPart(encrypted);
//create the message
MimeMessage message = new MimeMessage(session);
message.setSubject(mailSubject);
message.setHeader("Content-Type", "multipart/mixed");
message.setHeader("MIME-Version" , "1.0" );
message.setFrom(new InternetAddress(senderMailAddress));
message.addRecipients(Message.RecipientType.TO, address);
message.setContent(finalMultipart);
message.saveChanges();
trans.sendMessage(message, address);
// look for our recipient identifier
RecipientIdrecId = new RecipientId();
recId.setSerialNumber(cert.getSerialNumber());
recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
SMIMEEnveloped enveloped = new SMIMEEnveloped(baseMsg);
RecipientInformationStorerecipients = enveloped.getRecipientInfos();
RecipientInformationrecipient = recipients.get(recId);
......

