学习日记

正在关注 Linux 、开源的个人博客。

首页 | 留言本 | 标签云 | 网站地图 | 美味书签 | Learning Diary | 登录 |

增加订阅邮件功能已完成

2004年11月29日 下午 2:32 | 作者:raorao

我把发送邮件的原代码贴出来

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()

}

版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

相关日记

随机日记

添加到网摘

[del.icio.us]  [新浪 VIVI]  [365key]  [YouNote]  [博采中心]  [Poco]  [SOHU狐摘]  [天极网摘]  [和讯网摘] 

评论(2条评论)

  1. 祝贺你

  2. 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();

发表评论

*必填

*必填 (不会被公开)


Copyright © 2004-2008 www.learndiary.com(学习日记)
Powered by WordPress with theme by UCDCHINA for Blogool come from JunChen Wu, nowa
联系版主:mdx-xx at tom dot com。2004.7.25