Java Mail не может найти класс MimeMessage - PullRequest
0 голосов
/ 26 сентября 2018

Это код класса Mail (внутри есть основной, но по той простой причине, что таким образом решить эту проблему кажется простым):

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Mail {

    public static void main(String [] args) {
    // Recipient's email ID needs to be mentioned.
    String to = "abcd@gmail.com";

    // Sender's email ID needs to be mentioned
    String from = "mail";
    String psw = "password";

    // Assuming you are sending email from localhost
    String host = "localhost";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtps.host", host);
    properties.setProperty("mail.user", from);
    properties.setProperty("mail.password", psw);

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new                        InternetAddress(to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");

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

И этотерминал, который я вижу после запуска:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
    at Mail.main(Mail.java:35)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 1 more

Process finished with exit code 1

Ошибка включена:

Сообщение MimeMessage = новый MimeMessage (сеанс);

Ответы [ 2 ]

0 голосов
/ 03 октября 2018

Вам либо нужно указать JDK 9, что нужно открыть включенный, но скрытый модуль java.activation, либо явно включить jar-файл JavaBeans Activation Framework (JAF; javax.activation) в ваш проект.

Сделайте первое, добавив --add-modules java.activation к командной строке java.

Последнее можно сделать с помощью этой зависимости Maven:

<dependency>
  <groupId>com.sun.activation</groupId>
  <artifactId>javax.activation</artifactId>
  <version>1.2.0</version>
</dependency>
0 голосов
/ 26 сентября 2018

Попробуйте код ниже

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailTest {

    public static void main(String [] args) {
    // Recipient's email ID needs to be mentioned.
    String to = "tomail";

    // Sender's email ID needs to be mentioned
    String from = "frommail";
    String psw = "password";

    // different mail will have different host name, I have implemented using gmail
    String host = "smtp.gmail.com";
    String port = "587";

    Properties props = System.getProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
   // props.put("mail.smtp.connectiontimeout", timeout);
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");

    // Get the default Session object.
    //Session session = Session.getDefaultInstance(props);

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

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");

        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...