Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
当出现以上错误时,恭喜您已经离接收邮件不远了,否则请您解决好所有的异常后再来看这个帖子。
javax.mail和javax.activation这两个包已经在javaEE5当中属于基础包了,就是JDK中自带了已经,但是里面的方法与现在外面的mail.jar和activation.jar有一些出入,所以初学者在直接copy别人代码的时候往往会出现上面的错误。
废话不多说下面是解决方法
进到
X:\Program Files\MyEclipse 6.5\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806\data\libraryset\EE_5
这个路径里,可以看到javaee.jar,用rar把这个文件打开,然后进到javax文件夹里,删除mail.jar和activation.jar(我的javaee.jar里,这两个东西是文件夹,总之删掉就OK,不过要注意备份一下)
package com.web.action;
/**
*
*
* @author linfan
* @date Dec 22, 2008
*/
import java.io.UnsupportedEncodingException;
import javax.activation.DataHandler;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
/**
* 发送邮件测试通过
*
* @author He Jianfei
*/
public class JavaMail {
private String host;
private String from;
private String to;
private String subject;
private String content;
private boolean authentication;
private String username;
private String password;
public static void main(String[] args) throws EmailException {
SimpleEmail email = new SimpleEmail();
//设置发送主机的服务器地址
email.setHostName("smtp.126.com");
//设置收件人邮箱
email.addTo("vanadiumlin@gmail.com", "dddd");
//发件人邮箱
email.setFrom("vanadiumlin@126.com", "dddd");
//如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码
email.setAuthentication("vanadiumlin", "000");
//设置邮件的主题
email.setSubject("系统密码重置");
String body = "用户: 密码:";
//邮件正文消息
email.setContent(body, "text/plain;charset=gbk");
email.send();
System.out.println("The SimpleEmail send sucessful!!!");
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isAuthentication() {
return authentication;
}
public void setAuthentication(boolean authentication) {
this.authentication = authentication;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}