How to use properly Java Mail in Karaf for sending emails

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

How to use properly Java Mail in Karaf for sending emails

How to use properly Java Mail in Karaf for sending emails

I am developing a software that sends email in client-server setup.
Wherein the client runs in Apache Felix sends a request to the server that runs in Apache Karaf
to send an email to a given email address.

We had this setup since the client will just be connected to the intranet and
the server is the one that's connected to internet most of the time.
Thus will be responsible for sending emails.
As of the moment, I am just testing the SMTP server of google with host "".

I just followed the typical examples I found in the internet, my code looks like this:

{    String admin_email = "";
            String password = "dummypassword";
            String hostname = "";
            int port = 465;

            Properties props = System.getProperties();
            props.put("mail.debug", "true");
            props.put("mail.smtps.debug", "true");
            props.put("", hostname);
            props.put("mail.smtps.starttls.enable", "true");
            props.put("", "*");
            props.put("mail.smtps.auth", "true");
            props.put("mail.smtps.port", port);
            props.put("mail.smtps.socketFactory.class", "");
            props.put("mail.smtps.socketFactory.port", port);
            props.put("mail.smtps.socketFactory.fallback", "true");
            props.put("mail.smtps.connectiontimeout", "50000");
            props.put("mail.smtps.timeout", "50000");

            Authenticator authenticator = new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(admin_email, password);

            Session session = Session.getInstance(props, authenticator);

            String msgBody = "I have succesfully sent an email!";

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(admin_email));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("", false));
            msg.setSentDate(new Date());
            msg.setSubject("Your activation code is 123abc");

            SMTPTransport transport = (SMTPTransport) session.getTransport("smtps");
            try {
                transport.connect(hostname, admin_email, password);
                transport.sendMessage(msg, msg.getAllRecipients());
            } finally {

Deploying my_mail bundle which contains the code above
and has a dependency

works directly in Apache Felix when client is connected to the internet.
However, when I deploy the same setup in Karaf it gives me this error,

Caused by: SunTlsRsaPremasterSecret KeyGenerator not available

I tried other possible solutions to resolve the issue like :
1. use the existing javax.mail package by adding  javax.mail in the org.osgi.framework.system.packages.extra

2. add javax.mail in jre properties

3. use Apache Service Mix as dependency for my email bundle

but all these solutions would just give me this error:
 Exception or error caught by status service java.lang.NoClassDefFoundError: javax/mail/Authenticator

When I execute exports | grep mail I get this :

imports | grep javax.mail woul also look like this :

So for my questions:
1. I am suspecting that the duplicate javax.mail imports and exports are the one causing the error.
If so, is there a way to disable the javax.mail that is being used as dependency by the pax feature?

2. Is there an alternative way to send email in Karaf? instead of using my current implementation?

3. Can I use the existing instance of the javax.mail library without encountering the NoClassDef Error?
Can I use this javax.mail to its full extent?

I really hope that you can help me with this because I've been stuck with it for days now. :)
I would really appreciate any help rendered. ^__^
Thanks in advance! Good day.