Exception released when using the Commons-email API

Asked

Viewed 141 times

0

I’m trying to use the API Commons-Email as an alternative to the known JavaMail to send emails in Java but a problem is occurring. The following is being launched Exception:

Exception in thread "main" java.lang.Noclassdeffounderror: javax/mail/Message at Main.main(Main.java:6)

The problem occurs on line 6 when I try to instantiate a class object SimpleEmail. Just follow my code:

import org.apache.commons.mail.SimpleEmail;

public class Main {
    public static void main(String[] args) {
        try {
            SimpleEmail email = new SimpleEmail(); // ops?! Aqui é lançada a Exception
            email.setHostName("mail.foo.com");

            email.setFrom("[email protected]", "rnxn") // remetente
                 .addTo("[email protected]", "Sr. Foo") // destinatário
                 .setMsg("Exception sendo lançada ao utilizar a API Commons-email") // corpo do email
                 .setSubject("StackOverflow"); // título

            email.send(); // envio

        } catch(Exception e){
            System.out.println(e.getMessage());
        } 
    }
}

I have properly imported all files .jar that came in the download. Any solution for the treatment of this problem?

  • 1

    that is problems of Imports at least with me that error has always appeared because of it, sometimes some of the Lib’s is even correct but is in a different version than the one that is requested...

  • The problem is related to the import but it wasn’t because of the versions. Still I appreciate your comment.

  • 1

    Ta missing jars ai... He’s talking q did not find the Message class of javax.mail

1 answer

1


The problem is because the Commons-email is not an independent API as I thought, it is a more abstract layer of JavaMail API, i.e. it has dependencies.

The exception thrown in my code happens by the fact of not finding the class Message, belonging to JavaMail and not included in the archives .jar of with.

To solve the problem, all I had to do was download the JavaMail API through this link and do the import of the project together with the Commons-email.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.