问题
All I want to do is send an email in Java. Here is the example I found:
import org.apache.commons.mail.SimpleEmail;
public class Email {
public static void sendMessage(String emailaddress, String subject, String body) {
try {
SimpleEmail email = new SimpleEmail();
email.setHostName("valid ip address here");
email.addTo(emailaddress);
email.setFrom("noreply@example.com", "No reply");
email.setSubject(subject);
email.setMsg(body);
email.send();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I immediately get the following exception on the SimpleEmail email = new SimpleEmail();
line:
java.lang.ClassNotFoundException: org.apache.commons.mail.SimpleEmail
I have the following JAR in my project (using Netbeans):
commons-email-1.2.jar
What am I doing wrong?
Thanks.
回答1:
I tried to run your code and got this:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Message
at javaapplication3.Email.sendMessage(Email.java:9)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:7)
Caused by: java.lang.ClassNotFoundException: javax.mail.Message
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 2 more
Java Result: 1`
Appears that some import is missing. You need the javax.mail
package to run your code.
回答2:
Not sure why SimpleEmail didn't work. But this did:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class Email {
public static void sendMessage(String emailaddress, String subject, String body) {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "myhost");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail plain");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("test@example.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Thanks for the suggestions.
回答3:
The SimpleEmail class is in there, and you have the correct package, so I'd have to say this is a problem in NetBeans configuration for your project.
Since this is a simple example, you could try compiling from the command-line and adding the JAR to your classpath that way. It should work. Then you can figure out why it's not being picked up in NetBeans.
回答4:
You could also add the source code of the project to your src folder. But most of the times a clean & build helps with these problems.
If nothing helps, close netbeans, delete manually the build & dist folder of your project, start netbeans up again and perform a clean & build. Helped me once out of a mysterious problem.
来源:https://stackoverflow.com/questions/6847178/why-am-i-getting-this-exception-when-i-instantiate-a-simpleemail-class-in-java