STRUTS Action中調用自己寫的發郵件的類時出錯,但在類中用Main方法就可以成功!javamail
我在STRUTS用javamail ,儅我在ACTION中調用自己編寫的類MyEmail時,抛出了錯誤,NoClassDefFoundError! 但是我在MyEmail中寫了Main方法測試就可以成功!
Class MyEmail
///////////////////////////
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.struts.util.MessageResources;
public class MyMail {
public Session session = null;
//Email userName
String mailUser = "dong";
String host = "smtp.21cn.com";
String pwd = "XXXXXXXXX";
String test = "";
// String mailUser = "dong-boy";
// String host = "65.54.244.104";
// String pwd = "";
public MyMail(){
init();
}
public MyMail(String test){
this.test = test;
}
public void init(){
Properties props = new Properties();
props.put("mail.transpost.protocol","smtp");
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","25");
System.out.println("Autherticatorbean........before....");
Email_Autherticatorbean auth = new Email_Autherticatorbean(mailUser,pwd);
System.out.println("Autherticatorbean........after......");
session = Session.getInstance(props,auth);
//session = Session.getInstance(props);
session.setDebug(true);
}
public void sendMails(){
try{
Message msg = new MimeMessage(session);
//msg.setFrom();
msg.setFrom(new InternetAddress(test+"<dongdlhdlh@21cn.com>"));
// msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("dong-boy@21cn.com"));
// msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("dongdlhdlh@21cn.com"));
// msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("dongdlhdlh@yahoo.com.cn"));
// msg.setFrom(new InternetAddress("dong-boy@hotmail.com"));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("dong-boy@hotmail.com"));
//msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("dong-boy@21cn.com"));
msg.setSentDate(new Date());
msg.setSubject("this is my 3 mail "+new Date());
msg.setContent("this is my mail","text/html");
msg.setText("my success!!ha ha ha ha ~");
Transport transport = session.getTransport("smtp");
transport.connect(host,mailUser,pwd);
transport.send(msg);
}catch(SendFailedException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
// public static void main(String[] args){
// System.out.println("MyMail......");
// MyMail mymail = new MyMail();
// mymail.sendMails();
// }
}
//////////////
classEmail_Autherticatorbean
///////////////////////////////////////////
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticatorbean extends Authenticator {
private String m_username = null;
private String m_userpass = null;
public String getM_username() {
return m_username;
}
public void setM_username(String m_username) {
this.m_username = m_username;
}
public String getM_userpass() {
return m_userpass;
}
public void setM_userpass(String m_userpass) {
this.m_userpass = m_userpass;
}
public Email_Autherticatorbean(String username,String userpass){
super();
setM_username(username);
setM_userpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(m_username,m_userpass);
}