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.
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.
Not sure why SimpleEmail didn't work. But this did:
Thanks for the suggestions.
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.
I tried to run your code and got this:
Appears that some import is missing. You need the
javax.mail
package to run your code.