0

I need to send an email with attachment but I'm having the following error.

!ENTRY org.eclipse.rap.ui 4 0 2015-08-10 11:45:25.505
!MESSAGE Unhandled event loop exception
!STACK 0

java.lang.LinkageError: loader constraint violation: when resolving method "javax.mail.internet.MimeMessage.setDataHandler(Ljavax/activation/DataHandler;)V " the class loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) of the current class, com/fotgroup/tecas/ui/dialog/EmailSender, and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class, javax/mail/internet/MimeMessage, have different Class objects for the type setDataHandler used in the signature

at com.fotgroup.tecas.ui.dialog.EmailSender.sendMail(EmailSender.java:67)

I can't get the path of the image because is RCP and also this happens when I try to use DataHandler.

user207421
  • 305,947
  • 44
  • 307
  • 483

2 Answers2

1

It's hard to tell without the manifests of the bundles involved, but this looks like a framework setup error.

I can see that there's a JBoss modules ClassLoader and an Equinox ClassLoader in the mix here, and I'm guessing that you're starting an Equinox OSGi framework inside the JBoss modules framework. The rest of this answer assumes that this is what you're doing.

In order to make this work you will need to delegate loading of "JBoss provided packages" such as the Java EE API to the parent JBoss Modules framework, or otherwise you'll get inconsistent class space issues (such as the LinkageError you're seeing) when you call to JBoss server components.

The thing that you need to do is add to the framework system packages (the packages provided by the system bundle in the Equinox framework). You do this by specifying the org.osgi.framework.system.packages.extra property at startup.

The value of the property needs to include any packages that you want to add and their versions and and uses constraints (if you want things to work properly).

For example in this case you will need at least (and probably more than):

javax.mail;version=1.4;uses:="javax.activation,
   javax.mail.event,javax.mail.search",
javax.mail.event;version=1.4;uses:="javax.mail"
javax.mail.internet;version=1.4;uses:="javax.activation,
   javax.mail,javax.mail.util"
javax.mail.search;version=1.4;uses:="javax.mail,javax.mail.internet"
javax.mail.util;version=1.4;uses:="javax.activation,javax.mail.internet"

Note that the reason you need to do this isn't actually OSGi's fault. You're trying to access non-standard (i.e. non JRE) packages that exist outside the framework from inside the framework. OSGi's job is to enforce the runtime dependencies of your code, and this would normally mean "bundles you have deployed" in this case you're accessing packages that aren't coming from bundles, so you need to tell OSGi what they are.

Tim Ward
  • 1,169
  • 8
  • 9
0

I found the solution, to avoid using DataHandler, I use the following code

try {
MimeMessage m = new MimeMessage(getEmailSession());
InternetAddress[] to = new InternetAddress[] { new  InternetAddress("mail@domain.com") };
        m.setRecipients(Message.RecipientType.TO, to);
        if (valideMail) {
            m.setSender(new InternetAddress(user.getEmail()));
        }
        MimeMultipart multiPart = new MimeMultipart("mixed");
        MimeBodyPart contentPart = new MimeBodyPart();
        contentPart.setText("text", "UTF-8");
        multiPart.addBodyPart(contentPart);
        MimeBodyPart attachmentPart = new MimeBodyPart();
        attachmentPart.setContent(emailEntity.getAttachment(), "application/png");
        attachmentPart.addHeader("Content-Transfer-Encoding", "base64");
        attachmentPart.setDisposition(Part.ATTACHMENT);
        attachmentPart.setFileName(fileName);
        multiPart.addBodyPart(attachmentPart);
        m.setContent(multiPart);
        //
        Transport.send(m);
        //
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }