我正在尝试运行一个程序来使用 javamail 发送邮件。
它编译成功,但是当我运行它时,它给出了下面提到的错误。
线程“主”java.lang.NoClassDefFoundError 中的异常:javamail1_4_5/SendEma il(错误名称:SendEmail)
问题
- 除了在 classpath 中复制 mail.jar 和 activation.jar 之外,运行该程序是否需要任何设置。我已经复制了 C:\Program Files\Java\jdk1.7.0\jre\lib\ext 和 .java 文件中的这两个文件位于 java\jdk1.7.0\bin 文件夹中。
2.执行这个程序是否需要任何服务器?
这是代码
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abc@gmail.com";
// Sender's email ID needs to be mentioned
String from = "xyz@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}