当我在 Java 中实例化 SimpleEmail 类时,为什么会出现此异常?
我想做的就是用 Java 发送一封电子邮件。这是我找到的示例:
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("[email protected]", "No reply");
email.setSubject(subject);
email.setMsg(body);
email.send();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我立即在 SimpleEmail email = new SimpleEmail();
行上收到以下异常:
java.lang.ClassNotFoundException: org.apache.commons.mail.SimpleEmail
我的项目中有以下 JAR(使用 Netbeans): commons-email-1.2.jar
我做错了什么?
谢谢。
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("[email protected]", "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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试运行您的代码并得到以下信息:
似乎缺少某些导入。您需要
javax.mail
包来运行您的代码。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.不知道为什么 SimpleEmail 不起作用。但这做到了:
感谢您的建议。
Not sure why SimpleEmail didn't work. But this did:
Thanks for the suggestions.
SimpleEmail 类就在那里,并且您有正确的包,所以我不得不说这是您项目的 NetBeans 配置中的问题。
由于这是一个简单的示例,因此您可以尝试从命令行进行编译并将 JAR 添加到类路径中。它应该有效。然后您就可以弄清楚为什么它没有在 NetBeans 中被拾取。
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.
您还可以将项目的源代码添加到 src 文件夹中。但大多数时候都是干净的build 有助于解决这些问题。
如果没有帮助,请关闭 netbeans,手动删除构建和构建。项目的 dist 文件夹中,再次启动 netbeans 并执行 clean &建造。曾经帮助我解决了一个神秘的问题。
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.