cancel
Showing results for 
Search instead for 
Did you mean: 

Sending attachments with javax.mail

ChrisPaine
Active Contributor
0 Kudos

Hi,

I'm having a bit of difficulty as it appears that the DataHandler class referenced in com.sap.javax.mail-1.41.jar javax.mail.internet.MimeBodyPart.setDataHandler is different to the one that is referenced in com.sap.javax.activation-1.1.1.jar

thus I'm getting:

java.lang.LinkageError: loader constraint violation: when resolving method "javax.mail.internet.MimeBodyPart.setDataHandler(Ljavax/activation/DataHandler;)V" the class loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) of the current class, au/com/discoveryconsulting/timesheet/demo/mail/MailEventSender, and the class loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) for resolved class, javax/mail/internet/MimeBodyPart, have different Class objects for the type n/DataHandler;)V used in the signature

my build path has the SAPNWCloud components above the JRE so the SAP libs should be the ones that are being referenced and not the ones from the JDK.

I'm currently running SDK 1.12 - so will update to 1.17 and see if that makes a difference...

this is the code that's dying:

MimeBodyPart sigAttachment = new MimeBodyPart();

                              mainPart.addBodyPart(sigAttachment);

 

                              FileDataSource sigDS = new FileDataSource(

                                                  "/images/Discovery_email_sig.jpg");

                              sigAttachment.setDataHandler(new DataHandler(sigDS)); //this line here!

Any pointers much appreciated.

Cheers,


Chris

Accepted Solutions (1)

Accepted Solutions (1)

ChrisPaine
Active Contributor
0 Kudos

OK - so the file source is unlikely to have worked either...

I managed to solve it like this:

MimeBodyPart sigAttachment = new MimeBodyPart();

                              mainPart.addBodyPart(sigAttachment);

                              // create the details for the sig content

                              String embeddedAttachmentId = UUID.randomUUID().toString();

                              String mailHTMLWithSig = "<html><body>" + mailHTML

                                                  + "<p><img src=\"cid:" + embeddedAttachmentId

                                                  + "\" alt=\"ATTACHMENT\"></p></body></html>";

                              String sigPath = context

                                                  .getRealPath("/images/Discovery_email_sig.jpg");

                              File sigFile = new File(sigPath);

                              sigAttachment.attachFile(sigFile);

                              sigAttachment.setContentID("<" + embeddedAttachmentId + ">");

                              sigAttachment.setHeader("Content-Type", "image/jpg");

                              sigAttachment.setFileName(sigFile.getName());

where context is a reference to ServletContext which is got from a servlet with this.getServletContext();

Former Member
0 Kudos

Hi Chris,

this is a bug in the current version of the com.sap.javax.mail bundle (1.4.1).

Happy to hear you found a workaround in the meantime.

Independent of that, we will update the com.sap.javax.mail bundle to version 1.4.5 which will fix this issue. This will go into the SDK version 2.0.x, hopefully soon.

Sorry for any inconvenience,

Jan

ChrisPaine
Active Contributor
0 Kudos

Thanks Jan,

will be great to have a fully working version, although with this workaround I'm sorted, but I certainly was confused for a while there!

Former Member
0 Kudos

In case someone else has got this issue, there's another possible solution, which is based on using mime multipart. Something like this can be used:

MimeMultipart multiPart = new MimeMultipart("mixed");

MimeBodyPart contentPart = new MimeBodyPart();

contentPart.setText("Email Body", "UTF-8");

multiPart.addBodyPart(contentPart);

MimeBodyPart attachmentPart = new MimeBodyPart();

attachmentPart.setContent(pdfBytes, "application/pdf");

attachmentPart.addHeader("Content-Transfer-Encoding", "base64");

attachmentPart.setDisposition(Part.ATTACHMENT);

attachmentPart.setFileName("test_file.pdf");

multiPart.addBodyPart(attachmentPart);

where pdfBytes is a byte array containing a pdf attachment.

This works for me using SDK 1.31

Cheers,

Luigi

Answers (1)

Answers (1)

ChrisPaine
Active Contributor
0 Kudos

Same issue when using SDK1.17.0