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?