9

I want to send email, My code is below

public static void main(String[] args) {

    final String username = "emailId@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("email1@google.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

POM.xml

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.5</version>
</dependency>

But getting exception

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:230)
at javax.mail.Session.<init>(Session.java:214)
at javax.mail.Session.getInstance(Session.java:251)
at com.smart21.spring.utils.MailTest.main(MailTest.java:26)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Gnik
  • 7,120
  • 20
  • 79
  • 129
  • 1
    Try `com.sun.mail javax.mail 1.5.5`. I assume, you should use both dependecies. The issue is that java.mail-api doesn't have implementation. – Sergei Sirik Jul 25 '17 at 19:35
  • 1
    Yes, that's described on the [JavaMail project page](https://javaee.github.io/javamail/#Download_JavaMail_Release), and the latest release is 1.6.0. – Bill Shannon Jul 25 '17 at 19:52
  • 1
    Does this answer your question? [java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail](https://stackoverflow.com/questions/16807758/java-lang-noclassdeffounderror-com-sun-mail-util-maillogger-for-junit-test-case) – Steve Chambers Jul 16 '20 at 08:54

4 Answers4

13

Try changing from:

<groupId>javax.mail</groupId>

to:

<groupId>com.sun.mail</groupId>
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
2

This is a duplicate of the issue in java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail and the answer that worked for me:

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

See https://stackoverflow.com/a/16808343/1355930

Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23
1

The solution is to use correct dependency. For gradle:

// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.5.0-b01'
Jackkobec
  • 5,889
  • 34
  • 34
1

Try taking activation.jar and mail.jar from the Oracle website. This would help you resolve the error ClassNotFoundException: com.sun.mail.util.MailLogger.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Yoshita Mahajan
  • 443
  • 4
  • 6