T
T
thatmaniscool2018-07-31 08:38:36
Java
thatmaniscool, 2018-07-31 08:38:36

When I try to send a letter, it throws an exception, what could be the reasons?

On the net, I found a small code that shows how to send mail to email.
Let's just say, I stupidly copied it, I added all the necessary libraries.
The code:

private static String USER_NAME = "****";  // GMail user name (just the part before "@gmail.com")
    private static String PASSWORD = "****"; // GMail password
    private static String RECIPIENT = "*****@inbox.ru";


  public static void main(String[] args) {
    System.out.println("Try to send email to yout mail");
    String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";
        sendFromGMail(from, pass, to, subject, body);
        System.out.println("Check your mail!");
  }
  
  
  private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }

Throws the following exception

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler
at JavaNioServer.Main.sendFromGMail(Main.java:37)
at JavaNioServer.Main.main(Main.java:21)
Caused by: java.lang. ClassNotFoundException: javax.activation.DataHandler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/ java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

I read about the problem and something came across the fact that starting with java 9, you can’t just send a letter to the mail like that. How to fix this exception?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-07-31
@sergey-gornostaev

Not added all the necessary libraries. The error "NoClassDefFoundError: javax/activation/DataHandler" means that there is no javax.mail-1.6.1.jar in the classpath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question