当我通过命令行运行此代码时,我得到了一个感知。当我通过Eclipse IDE运行它时它运行正常,没有例外。我无法弄清楚发生了什么,但它确实在会话声明中得到了例外。
public static void sendEmail(String sendFile) {
// Send the log file via email.
final String username = "firstname.lastname@mycompany.com";
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();
}
}
我得到的错误只有在命令行运行时才会出现:
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
任何可能导致此问题的想法?
答案 0 :(得分:0)
这是java开发人员面临的最常见问题。
com.sun.mail.util.MailLogger
中为该类设置类路径。如果您尝试运行上述程序或任何其他程序,可以使用
之类的语法 java -cp /relative_path_to_your_dependency_jars ClassName
在你的情况下是这样的。
java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass
类似于编译类
java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass.java
答案 1 :(得分:0)
这结果是类路径问题,也使用javax.mail而不是javax.mail-api。
问题现在解决了。