Quantcast

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
|  
Report Content as Inappropriate

How to use properly Java Mail in Karaf for sending emails

Rienette
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 "smtp.gmail.com".

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

{    String admin_email = "admin_email@gmail.com";
            String password = "dummypassword";
            String hostname = "smtp.gmail.com";
            int port = 465;

            Properties props = System.getProperties();
            props.put("mail.debug", "true");
            props.put("mail.smtps.debug", "true");
            props.put("mail.smtps.host", hostname);
            props.put("mail.smtps.starttls.enable", "true");
            props.put("mail.smtps.ssl.trust", "*");
            props.put("mail.smtps.auth", "true");
            props.put("mail.smtps.port", port);
            props.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            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() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(admin_email, password);
                }
            };

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

            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("receiver_email@gmail.com", false));
            msg.setSentDate(new Date());
            msg.setSubject("Your activation code is 123abc");
            msg.setText(msgBody);

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


Deploying my_mail bundle which contains the code above
and has a dependency
       
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.2</version>
        </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: java.security.NoSuchAlgorithmException: 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.
Loading...