0

i'm getting some problems with the "session" object while trying to send an Email, that's the code

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailSender {
  private String user;
  private String password;
  private String host;
  private String destinatario;
  private String oggetto;
  private String allegato;

  /**
   * 
  * Costruttore completo, richiede i parametri
  * di connessione al server di posta
  * @param user
  * @param password
  * @param host
  * @param mittente
  * @param destinatari
  * @param oggetto
  * @param allegati
  */
  public EmailSender(String user, String password, String host, 
                     String destinatario, 
                     String oggetto){

    this.user = user;
    this.password = password;
    this.host = host;
    this.destinatario = destinatario;
    this.oggetto = oggetto;
  }

  // Metodo che si occupa dell'invio effettivo della mail
  public void inviaEmail() {
    int port = 465; //porta 25 per non usare SSL

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.user", user);
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    // commentare la riga seguente per non usare SSL 
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.socketFactory.port", port);

    // commentare la riga seguente per non usare SSL 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getInstance(props, null);
    session.setDebug(true);

    // Creazione delle BodyParts del messaggio
    MimeBodyPart messageBodyPart1 = new MimeBodyPart();
    MimeBodyPart messageBodyPart2 = new MimeBodyPart();

    try{
      // COSTRUZIONE DEL MESSAGGIO
      Multipart multipart = new MimeMultipart();
      MimeMessage msg = new MimeMessage(session);

      // header del messaggio
      msg.setSubject(oggetto);
      msg.setSentDate(new Date());
      msg.setFrom(new InternetAddress(user));

      // destinatario
      msg.addRecipient(Message.RecipientType.TO,
      new InternetAddress(destinatario));

      // corpo del messaggio
      messageBodyPart1.setText("Ciao sono veramente euforico");
      multipart.addBodyPart(messageBodyPart1);

      // allegato al messaggio
      allegato = CreazioneFile.AggiungiDati();
      DataSource source = new FileDataSource(allegato);
      messageBodyPart2.setDataHandler(new DataHandler(source));
      messageBodyPart2.setFileName(allegato);
      multipart.addBodyPart(messageBodyPart2);

      // inserimento delle parti nel messaggio
      msg.setContent(multipart);

      Transport transport = session.getTransport("smtps"); //("smtp") per non usare SSL
      transport.connect(host, user, password);
      transport.sendMessage(msg, msg.getAllRecipients());
      transport.close();

      System.out.println("Invio dell'email Terminato");

    }catch(AddressException ae) {
      ae.printStackTrace();
    }catch(NoSuchProviderException nspe){
      nspe.printStackTrace();
    }catch(MessagingException me){
      me.printStackTrace();
    }
  }
}

An that's the Test Class:

import java.io.File;

public class TestEmail {
  public static void main(String[] args) {
      File allegato = new File(CreazioneFile.AggiungiDati());

    EmailSender email = new EmailSender(
                    "myuser",
                    "mypass", 
                    "smtp.gmail.com",                     
                    "sendToAddress", 
                    "Invio automatico email da Java"
                  );

    email.inviaEmail();
    System.out.println(allegato);
    allegato.delete();
  }
}

That's the stack error:

Exception in thread "main" java.lang.NoClassDefFoundError: com.sun.mail.util.MailLogger at javax.mail.Session.initLogger(Session.java:226) at javax.mail.Session.(Session.java:210) at javax.mail.Session.getDefaultInstance(Session.java:321)

The problem it's for sure about RAD and session,but i dont know where i've to set, and what i've to set, can u please help me?

DarioS
  • 111
  • 1
  • 13

1 Answers1

1

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.

The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform.

99% that you run your tests in SE environment which means what you have to bother about adding it manually to your classpath when running tests.

Hope below link will helpful for you

http://crunchify.com/java-mailapi-example-send-an-email-via-gmail-smtp/

Abhilash Arjaria
  • 142
  • 2
  • 10
  • I cant take as example that one, i'm using a RAD IDE, and there's no way to convert that project in maven... – DarioS Nov 23 '16 at 08:36