3

While sending email I am getting the following errors using commons-email-1.3.
I have downloaded and added external jar's to the project.
Please help me fix this problem!

package mypkg;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

public class sendingmail {
     public static void main(String[] args)  throws Exception {
            Email email = new SimpleEmail();
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator("myid","mypwd")); //Here is the error
            email.setDebug(false);
            email.setHostName("smtp.gmail.com");
            email.setFrom("me@gmail.com");
            email.setSubject("Hi");
            email.setMsg("This is a test mail ... :-)");
            email.addTo("you@gmail.com");
            email.setTLS(true);
            email.send();
            System.out.println("Mail sent!");

    }
}

The line that gives the error is

email.setAuthenticator(new DefaultAuthenticator("myid","mypwd"));

The error message is

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The type javax.mail.Authenticator cannot be resolved. It is indirectly referenced from required .class files
The method setAuthenticator(Authenticator) from the type Email refers to the missing type Authenticator at mypkg.mailtest.main(mailtest.java:13)

ShadowScripter
  • 7,314
  • 4
  • 36
  • 54
H4SN
  • 1,482
  • 3
  • 24
  • 43

3 Answers3

9

Please download the jars from the specified links

Activation JAR

Java Mail jar

SSC
  • 2,956
  • 3
  • 27
  • 43
4

You need both mail.jar and activation.jar in your classpath.

Javier
  • 12,100
  • 5
  • 46
  • 57
1

open file pom.xml, add code:

<dependencies>

  <!-- http://mvnrepository.com/artifact/org.apache.commons/commons-email -->   
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.4</version>
    </dependency>
</dependencies>
Minato
  • 463
  • 6
  • 12
  • Please explain the answers you provide. – Maciej Jureczko Oct 05 '17 at 08:48
  • http://mvnrepository.com/artifact/org.apache.commons/commons-email/1.4 Default of apache commons email. With Maven is as above. My english is not good. – Minato Oct 05 '17 at 16:21
  • Add this code so that it automatically downloads the required libraries from the internet. Here is using lib: commons-email. – Minato Oct 05 '17 at 21:48