Je reçois une exception lorsque j'exécute ce code via la ligne de commande. Lorsque je l'exécute via l'IDE Eclipse, il fonctionne correctement, sans exception. Je n'arrive pas à comprendre ce qui se passe, mais c'est définitivement l'exception à l'énoncé de la session.NoClassDefFoundError ne se produit que lors de l'exécution du code via la ligne de commande
public static void sendEmail(String sendFile) {
// Send the log file via email.
final String username = "[email protected]";
final String password = "myPassword";
// Strings that contain from, to, subject, body and file path to the attachment.
String from = username;
String to = username;
String subject = "Baggage URL failures - Please check";
String body = "FareDB Bad URL(s) Report. The following URL(s) could not be loaded...";
String filename = sendFile;
System.out.println("Mail header info set!");
// Set smtp properties
Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
System.out.println("SMTP properties set!");
Session session = Session.getInstance(properties, null);
System.out.println("Mail session declared!");
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
// Just me for testing
message.setRecipient(Message.RecipientType.TO, new InternetAddress(username));
message.setSubject(subject);
message.setSentDate(new Date());
System.out.println("Mail body info set!");
// Set the email body
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(body);
System.out.println("Mime message body set!");
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());
System.out.println("Mail attachment info set!");
// Add all parts of the email to Multipart object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
System.out.println("Mail parts added!");
// Send email
Transport.send(message);
System.out.println("Mail sent!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
L'erreur que je reçois est que quand il est exécuté à la ligne de commande:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:221)
at javax.mail.Session.<init>(Session.java:206)
at javax.mail.Session.getInstance(Session.java:242)
at VerifyUrl.sendEmail(VerifyUrl.java:186)
at VerifyUrl.main(VerifyUrl.java:50)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 5 more
Toutes les idées ce qui pourrait être la cause?
Copie possible de [Problème de messagerie Java avec Session.getInstance] (http://stackoverflow.com/questions/24807472/java-mail-issue-with-session-getinstance) –
Copie possible de [java.lang.NoClassDefFoundError : com/dim/mail/util/MailLogger pour le cas de test JUnit pour le courrier Java] (http://stackoverflow.com/questions/16807758/java-lang-noclassdeffounderror-com-sun-mail-util-maillogger-for-junit -test-case) –