我把发送邮件的原代码贴出来
public class ToSendMailOfThread implements Runnable{
private ArrayList userInfoList = null;
private ArticleInfo articleInfo=null;
private int goalID ;
private String userNameOfWriteDiary = null;
ToSendMailOfThread(ArrayList userInfoList,ArticleInfo articleInfo,int goalID,String userNameOfWriteDiary){
this.userInfoList = userInfoList;
this.articleInfo = articleInfo;
this.goalID = goalID;
this.userNameOfWriteDiary = userNameOfWriteDiary;
}
public String getGoalName() {
String goalName =null;
try{
LearnDiaryDB myDB = new LearnDiaryDB();
goalName = myDB.getGoalNameByID(goalID);
}catch (Exception e){
e.printStackTrace();
return goalName;
}
return goalName;
}
public void run(){
String host = "smtp.126.com";
String from = "learndiary@126.com";
String uername = "learndiary";
String password = "123456";
UserInfo userInfo = null;
String goalName = getGoalName();
java.sql.Timestamp now = new java.sql.Timestamp(System.currentTimeMillis());
StringBuffer bodyTextBuffer = new StringBuffer() ;
bodyTextBuffer.append("\n");
bodyTextBuffer.append("关于学习目标:");
bodyTextBuffer.append(goalName);
bodyTextBuffer.append("\n");
bodyTextBuffer.append(userNameOfWriteDiary);
bodyTextBuffer.append(" 在 ");
bodyTextBuffer.append(now);
bodyTextBuffer.append(" 写了如下日记: \n");
bodyTextBuffer.append("---------------------------------------------------------------- \n");
bodyTextBuffer.append("标题: ");
bodyTextBuffer.append(articleInfo.getArticleName());
bodyTextBuffer.append("\n");
bodyTextBuffer.append("内容: ");
bodyTextBuffer.append(articleInfo.getArticleText());
bodyTextBuffer.append("\n");
bodyTextBuffer.append("相关资源: ");
bodyTextBuffer.append(articleInfo.getArticleResource());
bodyTextBuffer.append("\n");
bodyTextBuffer.append("---------------------------------------------------------------- \n");
bodyTextBuffer.append("欢迎常来学习日记! http://www.learndiary.com");
bodyTextBuffer.append("\n");
String bodyText = bodyTextBuffer.toString();
EmailAuthenticator mailAuthenticator = new EmailAuthenticator(uername,password);
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
Iterator iterator = userInfoList.iterator();
while(iterator.hasNext()){
userInfo=(UserInfo)iterator.next();
String toMail = userInfo.getEmail();
String userName = userInfo.getUserName();
try{
Session session = Session.getInstance(props,(Authenticator)mailAuthenticator);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(toMail));
message.setSubject("学习日记您订阅的目标日记");
message.setText("您好!"+userName+bodyText);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,uername,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
//System.out.println("已发送一封邮件:"+toMail);
} catch (Exception e){
e.printStackTrace();
}
}//end of while
}//end of run()
}
祝贺你
When something has a Runnable interface, it simply means that it has a run( ) method, but there’s nothing special about that?it doesn’t produce any innate threading abilities, like those of a class inherited from Thread. So to produce a thread from a Runnable object, you must create a separate Thread object as shown in this example, handing the Runnable object to the special Thread constructor. You can then call start( ) for that thread, which performs the usual initialization and then calls run( ).
如:raorao写的ToSendMailOfGoalAction.java中的:
ToSendMailOfThread toSendMailOfThread = new ToSendMailOfThread(userInfoList,articleInfo,goalID,userNameOfWriteDiary);
Thread t = new Thread(toSendMailOfThread);
t.start();