1

The program

import java.util.*;  
import javax.activation.*;  
import javax.mail.*;  
import javax.mail.internet.*;  

public class SendMail  
{  
    public static void main(String[] args)  
    {  
        String host="xxxxxxxxxxx";  
        final String user="xxxxxxxxx@gmail.com";  
        String to="xxxxxxxx@gmail";  
        //Get the session object  
        Properties props = new Properties();  
        props.put("mail.smtp.host", host);  
        props.put("mail.smtp.auth", "false");  
        Session session = Session.getDefaultInstance(props);  
        //Compose the message  
        try {  
            MimeMessage message = new MimeMessage(session);  
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
            message.setSubject("Test Mail");  
            message.setText("Test Mail Body");  
            //send the message  
            Transport.send(message);  
            System.out.println("message sent successfully...");  
        }
        catch (MessagingException e)
        {
            e.printStackTrace();
        }  
    }  
}

The problem is the below error

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
        at javax.mail.Session.initLogger(Session.java:261)
        at javax.mail.Session.<init>(Session.java:245)
        at javax.mail.Session.getDefaultInstance(Session.java:356)
        at javax.mail.Session.getDefaultInstance(Session.java:396)
        at SendMail.main(SendMail.java:22)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 5 more

my classpath says javax.mail-api-1.6.0.jar;javax.mail-1.6.0.jar;

I have no clue what I am doing wrong. Any help is appreciated. Thanks

This is running on the command line. No IDE's. So nothing to do with Maven

searcot jabali
  • 438
  • 6
  • 16
  • Program was indented, but stackoverflow was throwing up problems. So I removed all indentation – searcot jabali Jan 17 '18 at 12:48
  • Ok, fixed it for you. – Michael Jan 17 '18 at 12:50
  • Do you add full error trace? Also, what kind of project type do you use? I think your project is not maven. – ramazankul Jan 17 '18 at 13:11
  • @ramazankul. I updated it with the full stacktrace. No, this is not on an IDE. This is on the command line – searcot jabali Jan 17 '18 at 13:19
  • Remove `javax.mail-api-1.6.0.jar` from your classpath, it is only for compiling against the API. `javax.mail-1.6.0.jar` contains the API classes as well + the reference implementation. – Mark Rotteveel Jan 17 '18 at 13:26
  • @MarkRotteveel Did as you suggested. Same error – searcot jabali Jan 17 '18 at 13:29
  • 1
    Your code works fine for me. Please show the exact commandlines you use to compile and to run the application. And specify which Java version (`java -version`) your are using. – Mark Rotteveel Jan 17 '18 at 13:35
  • Do you have any other jar files in your jre/lib/ext directory? – Bill Shannon Jan 17 '18 at 20:56
  • Possible duplicate of [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) –  May 01 '18 at 03:02

0 Answers0