我编译成功了,但是执行的时候出现如下错误:
java.lang.NoClassDefFoundError: TestEmail
Caused by: java.lang.ClassNotFoundException: TestEmail
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: TestEmail.  Program will exit.
Exception in thread "main" 
我的环境变量设置的为(CLASSPATH):
.;%JAVA_HOME%\lib;D:\java\lib\commons-email-1.1.jar;D:\java\lib\commons-lang-2.4.jar;D:\java\lib\mail.jar
JAVA_HOME也设置了,bin目录也在path变量中加入了,但是运行的时候就是出错,大家帮个忙吧,多谢了!
我的代码如下,是从网上找的:
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class mailSender {/**
   * @param args
   */
public static void main(String[] args) {
   //不要使用SimpleEmail,会出现乱码问题
    HtmlEmail email = new HtmlEmail();
    try{
       //这里是发送服务器的名字:,163的如下:
        email.setHostName("smtp.163.com");
        //编码集的设置
        email.setCharset("gbk");
        //收件人的邮箱
        email.addTo("[email protected]");
        //发送人的邮箱
        email.setFrom("[email protected]", "发送人名称");
        //如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
        email.setAuthentication("abcd","abcd");
        email.setSubject("下午3:00会议室讨论,请准时参加");
        //要发送的信息
         email.setMsg("下午3:00会议室讨论,请准时参加");
         //发送
         email.send();
   } catch (EmailException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }}}

解决方案 »

  1.   

    java.lang.NoClassDefFoundError: TestEmail 
    没找到这个TestEmail这个类啊,你再看看你的设置呢?
      

  2.   

    没找到Class,代码里没什么TestEmail
      

  3.   

    多谢大家帮助了!
    下面是我从java2核心技术里面复制的代码,我使用这段代码发送成功了,但是为什么收到的邮件是空白的啊,没有主题,没有内容,发件人和收件人也没有?
    /**
     * @version 1.00 1999-08-27
     * @author Cay Horstmann
     */import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
    import javax.swing.*;public class MailTest
    {  public static void main(String[] args)
       {  JFrame frame = new MailTestFrame();
          frame.show();
       }
    }class MailTestFrame extends JFrame
       implements ActionListener
    {  public MailTestFrame()
       {  setTitle("MailTest");
          setSize(300, 300);
          addWindowListener(new WindowAdapter()
             {  public void windowClosing(WindowEvent e)
                {  System.exit(0);
                }
             } );      getContentPane().setLayout(new GridBagLayout());      GridBagConstraints gbc = new GridBagConstraints();
          gbc.fill = GridBagConstraints.HORIZONTAL;
          gbc.weightx = 0;
          gbc.weighty = 0;      gbc.weightx = 0;
          add(new JLabel("From:"), gbc, 0, 0, 1, 1);
          gbc.weightx = 100;
          from = new JTextField(20);
          add(from, gbc, 1, 0, 1, 1);      gbc.weightx = 0;
          add(new JLabel("To:"), gbc, 0, 1, 1, 1);
          gbc.weightx = 100;
          to = new JTextField(20);
          add(to, gbc, 1, 1, 1, 1);      gbc.weightx = 0;
          add(new JLabel("SMTP server:"), gbc, 0, 2, 1, 1);
          gbc.weightx = 100;
          smtpServer = new JTextField(20);
          add(smtpServer, gbc, 1, 2, 1, 1);      gbc.fill = GridBagConstraints.BOTH;
          gbc.weighty = 100;
          message = new JTextArea();
          add(new JScrollPane(message), gbc, 0, 3, 2, 1);      response = new JTextArea();
          add(new JScrollPane(response), gbc, 0, 4, 2, 1);      gbc.weighty = 0;
          JButton sendButton = new JButton("Send");
          sendButton.addActionListener(this);
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(sendButton);
          add(buttonPanel, gbc, 0, 5, 2, 1);
       }   private void add(Component c, GridBagConstraints gbc,
          int x, int y, int w, int h)
       {  gbc.gridx = x;
          gbc.gridy = y;
          gbc.gridwidth = w;
          gbc.gridheight = h;
          getContentPane().add(c, gbc);
       }   public void actionPerformed(ActionEvent evt)
       {  SwingUtilities.invokeLater(new Runnable()
            {  public void run()
               {   sendMail();
               }
            });
       }   public void sendMail()
       {  try
          {  Socket s = new Socket(smtpServer.getText(), 25);         out = new PrintWriter(s.getOutputStream());
             in = new BufferedReader(new
                InputStreamReader(s.getInputStream()));         String hostName
                = InetAddress.getLocalHost().getHostName();         send(null);
             send("HELO " + hostName);
             send("MAIL FROM: " + from.getText());
             send("RCPT TO: " + to.getText());
             send("DATA");
             out.println(message.getText());
             send(".");
             s.close();
          }
          catch (IOException exception)
          {  response.append("Error: " + exception);
          }
       }   public void send(String s) throws IOException
       {  if (s != null)
          {  response.append(s + "\n");
             out.println(s);
             out.flush();
          }
          String line;
          if ((line = in.readLine()) != null)
             response.append(line + "\n");
       }   private BufferedReader in;
       private PrintWriter out;
       private JTextField from;
       private JTextField to;
       private JTextField smtpServer;
       private JTextArea message;
       private JTextArea response;
    }
      

  4.   

    TestEmail找不到,看看你的\WEB-INF\lib下面有没有commons-email-1.1.jar这个包
    下面的代码,如果你写上内容的话,应该是可以收到的,件名好像没有地方填吧。