javajardependenciessimple-java-mail

Class EmailAddressCriteria not found (SimpleJavaMail)


I try to use the SimpleJavaMail library, but I think I missed something when importing the JAR of the API and it's dependencies. I use Java 8 (OpenJDK), and here is a list of the external JARs I added in my Eclipse Oxygen project configuration :

Here is a code sample:

import org.simplejavamail.email.Email;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.Mailer;
import org.simplejavamail.mailer.MailerBuilder;
import org.simplejavamail.mailer.config.TransportStrategy;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Email notif = EmailBuilder.startingBlank()
                .to("someone@somewhere.fr")
                .withSubject("Bla")
                .withPlainText("Lorem ipsum\nLorem ipsum")
                .buildEmail();
        
        Mailer mailer = MailerBuilder
                .withSMTPServer("smtp.gmail.com", 587, "foo@gmail.com", "bar")
                .withTransportStrategy(TransportStrategy.SMTP_TLS)
                .withSessionTimeout(10 * 1000)
                .clearEmailAddressCriteria() // turns off email validation
                .withDebugLogging(true)
                .buildMailer();
        
        mailer.sendMail(notif);
    }
}

The exception I have running that code through Eclipse on my Linux is:

Exception in thread "main" java.lang.NoClassDefFoundError: org/hazlewood/connor/bottema/emailaddress/EmailAddressCriteria
    at org.simplejavamail.mailer.MailerGenericBuilder.<init>(MailerGenericBuilder.java:152)
    at org.simplejavamail.mailer.MailerBuilder$MailerRegularBuilder.<init>(MailerBuilder.java:136)
    at org.simplejavamail.mailer.MailerBuilder.withSMTPServer(MailerBuilder.java:49)
    at Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: org.hazlewood.connor.bottema.emailaddress.EmailAddressCriteria
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

Any idea on how to fix this?


Solution

  • Looking at Maven Central's pom.xml for simple-java-mail-5.0.3 you are missing the following dependency

     <dependency>
      <groupId>com.github.bbottema</groupId>
      <artifactId>emailaddress-rfc2822</artifactId>
      <version>1.0.1</version>
      <scope>compile</scope>
    </dependency>
    

    I think you should use a build tool like Maven or Gradle to set up your project instead of adding the JAR files manually to avoid problems with missing dependencies.