0

Estoy intentando que mi aplicación envíe mails utilizando java. Utilizo java.mail y jdk.17.0.2. Por más de que las credenciales son correctas y no jay ningún error dentro del código, me encuentro con la siguiente excepcion que no se cómo solucionar (Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler). Dejo el código donde se marca el error (Linea 55 al instanciar la clase MimeMessage). Si alguien puede ayudarme lo agreadezco. En caso de que tengan otra forma de generar mails desde java también me es útil, ya que no necesito utilizar este método especificamente sino que solo me es necesario poder generar los mails.

package plantilla;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
import java.util.Properties;

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;

public class Mail {

    private Properties properties;
    private Session session;

    public Mail(String ruta) throws IOException {
        this.properties = new Properties();
        loadConfig(ruta);

        session = Session.getDefaultInstance(properties);
    }

    private void loadConfig(String ruta) throws InvalidParameterException, IOException {
        InputStream is = new FileInputStream(ruta);
        this.properties.load(is);
        checkConfig();
    }

    private void checkConfig() throws InvalidParameterException {

        String[] keys = {
                "mail.smtp.host",
                "mail.smtp.port",
                "mail.smtp.user",
                "mail.smtp.password",
                "mail.smtp.starttls.enable",
                "mail.smtp.auth"
        };

        for (int i = 0; i < keys.length; i++) {
            if (this.properties.get(keys[i]) == null) {
                throw new InvalidParameterException("No existe la clave " + keys[i]);
            }
        }

    }

    public void enviarEmail(String asunto, String mensaje, String correo) throws MessagingException {

        MimeMessage contenedor = new MimeMessage(session);
        contenedor.setFrom(new InternetAddress((String) this.properties.get("mail.smtp.user")));
        contenedor.addRecipient(Message.RecipientType.TO, new InternetAddress(correo));
        contenedor.setSubject(asunto);
        contenedor.setText(mensaje);
        Transport t = session.getTransport("smtp");
        t.connect((String) this.properties.get("mail.smtp.user"), (String) this.properties.get("mail.smtp.password"));
        t.sendMessage(contenedor, contenedor.getAllRecipients());

    }

    public void enviarEmail(String asunto, String mensaje, String[] correos) throws MessagingException {

        MimeMessage contenedor = new MimeMessage(session);
        contenedor.setFrom(new InternetAddress((String) this.properties.get("mail.smtp.user")));
        for (int i = 0; i < correos.length; i++) {
            contenedor.addRecipient(Message.RecipientType.TO, new InternetAddress(correos[i]));
        }
        contenedor.setSubject(asunto);
        contenedor.setText(mensaje);
        Transport t = session.getTransport("smtp");
        t.connect((String) this.properties.get("mail.smtp.user"), (String) this.properties.get("mail.smtp.password"));
        t.sendMessage(contenedor, contenedor.getAllRecipients());

    }

}
1

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.