当我在 Java 中实例化 SimpleEmail 类时,为什么会出现此异常?

发布于 2024-11-26 15:29:22 字数 1013 浏览 2 评论 0原文

我想做的就是用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸伞微斜 2024-12-03 15:29:22

我尝试运行您的代码并得到以下信息:

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`

似乎缺少某些导入。您需要 javax.mail 包来运行您的代码。

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.

紧拥背影 2024-12-03 15:29:22

不知道为什么 SimpleEmail 不起作用。但这做到了:

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("[email protected]"));

           transport.connect();
           transport.sendMessage(message,
                   message.getRecipients(Message.RecipientType.TO));
           transport.close();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
     }
  }

感谢您的建议。

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("[email protected]"));

           transport.connect();
           transport.sendMessage(message,
                   message.getRecipients(Message.RecipientType.TO));
           transport.close();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
     }
  }

Thanks for the suggestions.

可可 2024-12-03 15:29:22

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.

挖鼻大婶 2024-12-03 15:29:22

您还可以将项目的源代码添加到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文