方法的代码:package com.bany.netmall.example;import java.util.Properties;import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test {
private MimeMessage mimemessage;
private Session session;
private static Properties properties;
private boolean needAuth = true;
private String username = "";
private String password = "";
private Multipart multipart; public Test(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
} public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (properties == null)
properties = System.getProperties(); //获得系统属性对象
properties.put("mail.smtp.host", hostName); //设置SMTP主机
public boolean createMimeMessage()
try {
System.out.println("准备获取邮件会话对象!");
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimemessage = new MimeMessage(session); //创建MIME邮件对象
multipart = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false; public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (properties == null)
properties = System.getProperties();
if (need) {
properties.put("mail.smtp.auth", "true");
} else {
properties.put("mail.smtp.auth", "false");
}
} public void setNamePass(String name, String pass) {
username = name;
password = pass;
} public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimemessage.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + mailBody, "text/html;charset=GB2312");
multipart.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimemessage.setFrom(new InternetAddress(from)); //设置发信人
return true;
} catch (Exception e) {
return false;
}
} public boolean setTo(String to) {
if (to == null)
return false;
try {
mimemessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
} }
public boolean sendout() {
try {
mimemessage.setContent(multipart);
mimemessage.saveChanges();
System.out.println("正在发送邮件....");
session = Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect((String) properties.get("mail.smtp.host"), username,password);
transport.sendMessage(mimemessage, mimemessage.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
} public static void main(String[] args) { String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + "<div align=center><a href=http://www.csdn.net>csdn</a></div>";
SendMailExample themail = new SendMailExample("smtp.163.com");
themail.setNeedAuth(true);
if (themail.setSubject("测试邮件") == false)
return;
if (themail.setBody(mailbody) == false)
return;
//改为接收邮件地址即可
if (themail.setTo("[email protected]") == false)
return;
//改为发送邮件地址即可
if (themail.setFrom("[email protected]") == false)
return;
/*if (themail.addFileAffix("c:\\test.txt") == false)
return;*/
themail.setNamePass("nuno_dns", "10bany12");
if (themail.sendout() == false)
return;
}
}
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test {
private MimeMessage mimemessage;
private Session session;
private static Properties properties;
private boolean needAuth = true;
private String username = "";
private String password = "";
private Multipart multipart; public Test(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
} public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (properties == null)
properties = System.getProperties(); //获得系统属性对象
properties.put("mail.smtp.host", hostName); //设置SMTP主机
public boolean createMimeMessage()
try {
System.out.println("准备获取邮件会话对象!");
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimemessage = new MimeMessage(session); //创建MIME邮件对象
multipart = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false; public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (properties == null)
properties = System.getProperties();
if (need) {
properties.put("mail.smtp.auth", "true");
} else {
properties.put("mail.smtp.auth", "false");
}
} public void setNamePass(String name, String pass) {
username = name;
password = pass;
} public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimemessage.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + mailBody, "text/html;charset=GB2312");
multipart.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimemessage.setFrom(new InternetAddress(from)); //设置发信人
return true;
} catch (Exception e) {
return false;
}
} public boolean setTo(String to) {
if (to == null)
return false;
try {
mimemessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
} }
public boolean sendout() {
try {
mimemessage.setContent(multipart);
mimemessage.saveChanges();
System.out.println("正在发送邮件....");
session = Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect((String) properties.get("mail.smtp.host"), username,password);
transport.sendMessage(mimemessage, mimemessage.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
} public static void main(String[] args) { String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + "<div align=center><a href=http://www.csdn.net>csdn</a></div>";
SendMailExample themail = new SendMailExample("smtp.163.com");
themail.setNeedAuth(true);
if (themail.setSubject("测试邮件") == false)
return;
if (themail.setBody(mailbody) == false)
return;
//改为接收邮件地址即可
if (themail.setTo("[email protected]") == false)
return;
//改为发送邮件地址即可
if (themail.setFrom("[email protected]") == false)
return;
/*if (themail.addFileAffix("c:\\test.txt") == false)
return;*/
themail.setNamePass("nuno_dns", "10bany12");
if (themail.sendout() == false)
return;
}
}
HttpServletResponse response) throws ServletException, IOException {
Test test = new Test("smtp.163.com");
test.setNeedAuth(true);
if (test.setSubject("测试邮件") == false)
return;
if (test.setBody("csdn") == false)
return;
if (test.setTo("[email protected]") == false)
return;
if (test.setFrom("[email protected]") == false)
return;
test.setNamePass("456","456");
if (test.sendout() == false)
return;
}
com.bany.netmall.servlet.member.MemberRetake.doGet(MemberRetake.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)为什么总是找不到指定的类呢??
配置的环境没有问题啊
工程里已经导入了javamail和jaf的包了
导入JAF的包不就行了吗?要是需要安装,怎么安装啊?介绍介绍
如下:
java.lang.NoClassDefFoundError: javax/mail/Multipart
com.bany.netmall.servlet.member.MemberRetake.doGet(MemberRetake.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
import java.util.*;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.accessibility.*;
public class sendmail {
String from="[email protected]";
String to="[email protected]";
String subject="nihao";
String text="hello,2005"; /*
String to="";
String subject="";
String text="";
*/
String user="";
String passwd="";
String smtp="smtp.fasoft.com.cn"; Properties props =null;
Session sendMailSession=null;
Transport transport=null; public sendmail() {
props = new Properties();
} public void setParam(String to,String subject,String text){
this.to=to;
this.subject=subject;
this.text=text;
} public void setProtocol(String user,String passwd){
this.user=user;
this.passwd=passwd;
} public void setFrom(String from){
this.from=from;
} public boolean sendto(){
try{
props.put("mail.smtp.host",smtp);
sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(text); transport = sendMailSession.getTransport("smtp"); transport.connect(smtp,user,passwd);
transport.send(newMessage);
transport.close();
return true;
}catch(MessagingException m){
// System.out.println(m.toString());
return false;
}
}
public static void main(String[] args) {
sendmail sendmail1 = new sendmail();
System.out.println(sendmail1.sendto());
}
}
package com.bany.netmall.example;import java.util.*;import javax.mail.*;
import javax.mail.Address;
import javax.mail.internet.*;
import javax.activation.*;/**
* @author Bany.Gun
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SendMailExample { private MimeMessage mimemessage;
private Session session;
private static Properties properties;
private boolean needAuth = true;
private String username = "";
private String password = "";
private Multipart multipart; public SendMailExample(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
} public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (properties == null)
properties = System.getProperties();
properties.put("mail.smtp.host", hostName);
} public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimemessage = new MimeMessage(session);
multipart = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
} public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (properties == null)
properties = System.getProperties();
if (need) {
properties.put("mail.smtp.auth", "true");
} else {
properties.put("mail.smtp.auth", "false");
}
} public void setNamePass(String name, String pass) {
username = name;
password = pass;
} public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimemessage.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
} public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>" + mailBody, "text/html;charset=GB2312");
multipart.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
} public boolean addFileAffix(String filename) { System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
multipart.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
} public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimemessage.setFrom(new InternetAddress(from));
return true;
} catch (Exception e) {
return false;
}
} public boolean setTo(String to) {
if (to == null)
return false;
try {
mimemessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
} } public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimemessage.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
} public boolean sendout() {
try {
mimemessage.setContent(multipart);
mimemessage.saveChanges();
System.out.println("正在发送邮件....");
session = Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect((String) properties.get("mail.smtp.host"), username,password);
transport.sendMessage(mimemessage, mimemessage.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
} public static void main(String[] args) { SendMailExample themail = new SendMailExample("smtp.163.com");
themail.setNeedAuth(true);
if (themail.setSubject("测试邮件") == false)
return;
if (themail.setBody("test") == false)
return;
if (themail.setTo("[email protected]") == false)
return;
if (themail.setFrom("[email protected]") == false)
return;
if (themail.addFileAffix("c:\\test.txt") == false)
return;
themail.setNamePass("123", "123");
if (themail.sendout() == false)
return;
}
}
这是我改的发送邮件的方法,测试了,可以发送而且邮箱中可以收到发送的邮件,现在的问题就是一用servlet调用就不行,抱错好像是找不到指定的类
java.lang.NoClassDefFoundError: javax/mail/Address
com.bany.netmall.servlet.member.MemberRetake.doPost(MemberRetake.java:35)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
各位帮我分析一下