2

I'm getting an excception when I run this code via the command line. When I run it via the Eclipse IDE it runs fine, no exception. I can't figure out what's going on but it's definitely getting the exception at the Session statement.

public static void sendEmail(String sendFile) {
    // Send the log file via email.
    final String username = "firstname.lastname@mycompany.com";
    final String password = "myPassword";

    // Strings that contain from, to, subject, body and file path to the attachment.
    String from = username;
    String to = username;
    String subject = "Baggage URL failures - Please check";
    String body = "FareDB Bad URL(s) Report. The following URL(s) could not be loaded...";
    String filename = sendFile;
    System.out.println("Mail header info set!");

    // Set smtp properties
    Properties properties = new Properties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    System.out.println("SMTP properties set!");

    Session session = Session.getInstance(properties, null);
    System.out.println("Mail session declared!");

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        // Just me for testing
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(username));
        message.setSubject(subject);
        message.setSentDate(new Date());
        System.out.println("Mail body info set!");

        // Set the email body
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(body);
        System.out.println("Mime message body set!");

        // Set the email attachment file
        MimeBodyPart attachmentPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename) {
            @Override
            public String getContentType() {
                return "application/octet-stream";
            }
        };
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());
        System.out.println("Mail attachment info set!");

        // Add all parts of the email to Multipart object
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);
        System.out.println("Mail parts added!");

        // Send email
        Transport.send(message);
        System.out.println("Mail sent!");
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

The error I get is only when it's run at the command line:

 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:221)
    at javax.mail.Session.<init>(Session.java:206)
    at javax.mail.Session.getInstance(Session.java:242)
    at VerifyUrl.sendEmail(VerifyUrl.java:186)
    at VerifyUrl.main(VerifyUrl.java:50)
 Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 5 more

Any ideas what could be causing this?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Sulteric
  • 505
  • 6
  • 16
  • 1
    Possible duplicate of [Java Mail Issue with Session.getInstance](http://stackoverflow.com/questions/24807472/java-mail-issue-with-session-getinstance) – Joshua Howard Dec 19 '16 at 19:14
  • 1
    Possible duplicate of [java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail](http://stackoverflow.com/questions/16807758/java-lang-noclassdeffounderror-com-sun-mail-util-maillogger-for-junit-test-case) – Mr. Polywhirl Dec 19 '16 at 19:15

2 Answers2

0

This is most common problem java developer faces.

  • Where java has package structure mechanism.
  • To run any java program which is using other package classes/interfaces/enums, you need to import it to class.
  • And If you are using external library or other packages then you need to set class path for that class in your case com.sun.mail.util.MailLogger.
  • where IDE like eclipse and IntellijIdea do packaging and setting classpath for you.
  • If you are trying to run above program or any other program you can follow syntax like

    java -cp /relative_path_to_your_dependency_jars ClassName

    In your case something like this.

    java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass

    similarly for compiling class

    java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass.java

Gopinath Langote
  • 311
  • 4
  • 10
  • That's the thing, my classpath IS set properly. I'll have to keep looking at this. It makes no sense at all. – Sulteric Dec 20 '16 at 14:43
  • com.sun.mail.util.MailLogger is part of JavaMail API. It is already included in EE environment (that's why you can use it on your live server), but it is not included in SE environment. – Gopinath Langote Dec 20 '16 at 14:47
  • I'm a beginner at Java so I don't understand 1% of what you're saying. I've tried everything I can understand at the links posted above but I'm still having the issue. If someone could put this in plain english I'd appreciate it. And please don't post XML stuff without telling me what file it's supposed to go in and where it's located. Again, my classpath is fine. It works in Eclipse fine. Anything I have included in Eclipse is in my classpath. I have eliminated the javax-api jar file and included the javax.mail.jar file instead. Still doesn't work. – Sulteric Dec 20 '16 at 15:18
  • And it's not activation.jar missing. I just added that to my classpath and still getting the same error. – Sulteric Dec 20 '16 at 16:18
  • will you please add entire class impelmantation at least import statements – Gopinath Langote Dec 20 '16 at 16:22
0

This turned out to be a classpath issue and also using javax.mail instead of javax.mail-api.

Issue is solved now.

Sulteric
  • 505
  • 6
  • 16