我成功地构建了我的代码,然后我有一个问题。
这是我的代码:
package EmailSending;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class launcher {
public static void main(String[] args){
final String username = "my-email@yandex.ru";
final String password = "my-password";
String to = "to-email@mail.ru";
String from = "from-email@yandex.ru";
String host = "localhost";
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
Session session = Session.getInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
这行有一个错误:Session session = Session.getInstance(properties);
这是一条输出消息:
run:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:227)
at javax.mail.Session.<init>(Session.java:212)
at javax.mail.Session.getInstance(Session.java:265)
at EmailSending.launcher.main(launcher.java:34)
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 4 more
Java Result: 1
我如何在NetBeans中观看完整的输出错误消息?如何扩大"……4多"?
如果您还没有弄清楚,那么该异常的原因是您没有将正确的JavaMail库添加到项目中。请看这个JavaMail FAQ条目。
如果您正在使用Maven,您可能希望引用这里列出的JavaMail的Maven坐标。你想要com.sun.mail:javax.mail.