質問

I built my code successfully and then I had a problem.

This is my code:

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

public class launcher {
    public static void main(String[] args){

      final String username = "my-email@yandex.ru";
      final String password = "my-password";

      String to = "to-email@mail.ru";

      String from = "from-email@yandex.ru";

      String host = "localhost";

      Properties properties = System.getProperties();
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.host", host);

      Session session = Session.getInstance(properties);

      try{

         MimeMessage message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from));

         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         message.setSubject("This is the Subject Line!");

         message.setText("This is actual message");

         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}


There is an error at this line:
Session session = Session.getInstance(properties);

This is an output message:

run:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.<init>(Session.java:212)
    at javax.mail.Session.getInstance(Session.java:265)
    at EmailSending.launcher.main(launcher.java:34)
    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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more
Java Result: 1



How can I watch a full output error message in NetBeans? How to expand "...4 more"?

役に立ちましたか?

解決

In case you haven't figured it out, the cause of that exception is that you haven't added the right JavaMail library to your project. See this JavaMail FAQ entry.

If you're using Maven, you might want to refer to the Maven coordinates for JavaMail listed here. You want com.sun.mail:javax.mail.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top