发新话题
打印

[JAVA程序] Java写的收POP3程序command-line界面的『转自黑基意识』

Java写的收POP3程序command-line界面的『转自黑基意识』

利用JavaMail和JAF


1。这是类用来保持POP3下载的单条邮件

/***************************************************************
*It's the class wich contain the message downlload from the POP3
*
****************************************************************/



import javax.mail.*;
import java.io.*;


class Mail implements Serializable{
        ** boolean isRead;
        ** boolean isJunk;
        ** boolean isDel;
        ** boolean realDel;
        ** Message message;

        public Mail(Message m){
                        isRead=false;
                        isJunk=false;
                        isDel=false;
                        realDel=false;
                        message=m;
        }

        public void setIsRead(boolean flag){
                isRead=flag;
        }

        public void setIsJunk(boolean flag){
                isJunk=flag;
        }

        public void SetIsDel(boolean flag){
                isDel=flag;
        }

        public void SetRealDel(boolean flag){
                realDel=flag;
        }

        public boolean getIsRead(){
                return isRead;
        }

        public boolean getIsJunk(){
                return isJunk;
        }

        public boolean getIsDel(){
                return isDel;
        }

        public boolean getRealDel(){
                return realDel;
        }

        public Message getMessage(){
                return message;
        }

TOP

2。这个类用来存放邮件的帐户信息

/***************************************************************
*This class contain the account infomation
*
***************************************************************/


import java.io.*;

class AccountInfo implements Serializable{
        ** static int indexNo=1;

        ** String accountNo;
        ** String accountName;
        ** String host;
        ** String userName;
        ** String password;

        public AccountInfo(String name,String h, String user, String psw){
                String title="A000";
                int temp=1000+indexNo;
                accountNo=title+temp;
                accountName=name;
                host=h;
                userName=user;
                password=psw;
        }



        public void setAccountNo(String an){
                accountNo=an;
        }

        public void setAccountName(String name){
                accountName=name;
        }

        public void setHost(String h){
                host=h;
        }

        public void setUser(String user){
                userName=user;
        }

        public void setPass(String pass){
                password=pass;
        }

        public String getAccountNo(){
                return accountNo;
        }

        public String getAccountName(){
                return accountName;
        }

        public String getHost(){
                return host;
        }
        public String getUser(){
                return userName;
        }

        public String getPass(){
                return password;
        }

}

TOP

3。 这个类用来存放某个帐户下的所以邮件


/************************************************************
*This class contain the the the mails under the account
*
**************************************************************/


import java.util.ArrayList;
import java.io.*;



class Account implements Serializable{

        ** AccountInfo info;

        ** ArrayList mailList;

        public Account(AccountInfo ai){
                setInfo(ai);
                mailList=new ArrayList();

        }

        public void setInfo (AccountInfo ai){
                info=ai;
        }

        public AccountInfo getInfo(){
                return info;
        }

        public ArrayList getMailList(){
                return mailList;
        }

        public boolean addMail(Mail[] mes){
                boolean flag=true;

                for(int i=1;i<mes.length;i++){
                        flag=mailList.add(mes);
                        if(flag==false){
                                return flag;
                        }
                }
                return true;
        }

        public Mail[] getMail(){
                Object[] temp=mailList.toArray();
                Mail[] mes=new Mail[temp.length];
                for(int i=0;i<mes.length;i++){
                        mes=(Mail)temp;
                }
                return mes;
        }




}

TOP

4。这个类存放所以的帐户和邮件


/**********************************************************
*This class contains the the accounts and the mails
*
***********************************************************/


import java.util.HashMap;
import java.util.Vector;
import javax.mail.*;
import java.io.*;

class MailBox implements Serializable{

        ** HashMap mailBox;
        ** Vector accountHold;

        ** int size;

        public MailBox(){
                mailBox=new HashMap();
                accountHold=new Vector();
                size=0;
        }
        public int getSize(){
                return size;
        }

        public void setSize(){
                size=mailBox.size();
        }

        public HashMap getMailBox(){
                return mailBox;
        }

        public void addAccount(Account ac){
                accountHold.add(ac.getInfo());
        }

        public AccountInfo getAccount(String acNo){
                AccountInfo temp;

                for(int i=0;i<accountHold.size();i++){
                        temp=(AccountInfo)accountHold.get(i);
                        if(temp.getAccountNo().equals(acNo)){
                                return temp;
                        }
                }
                return null;
        }

        public AccountInfo[] getAllAccount(){
                 AccountInfo[] allAccount=new AccountInfo[accountHold.size()];
                 for(int i=0;i<accountHold.size();i++){
                         allAccount=(AccountInfo)accountHold.get(i);
                 }
                 return allAccount;
         }




        public void addMail(Account ac){
                 mailBox.put(ac.getInfo().getAccountNo(),ac);
        }

        public Account getMail(String acNo){
                Account temp=(Account)mailBox.get(acNo);
                return temp;
        }

        public void delMail(String acNo){
                mailBox.remove(acNo);
        }



}

TOP

5。这个类真正完成收信的功能

/****************************************************
* This class actually receive the email
*
*****************************************************/


import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

class MailDemo{

        //** AccountInfo info;

        ** String host;
        ** String username;
        ** String password;

        ** Session session;
        ** Store store;
        ** Folder folder;
        ** Message[] message;

        ** String sender;
        ** String subject;
        ** String content;

        public void setSender(String s){
                sender=s;
        }

        public void setSubject(String su){
                subject=su;
        }

        public void setContent(String c){
                content=c;
        }

        public String getSender(){
                return sender;
        }

        public String getSubject(){
                return subject;
        }

        public String getContent(){
                return content;
        }

        public Message[] getMessage(){
                return message;
        }

        public MailDemo(AccountInfo info){
                host=info.getHost();
                username=info.getUser();
                password=info.getPass();

                Properties props=new Properties();
                session=Session.getDefaultInstance(props,null);
        }

        public void getMail()throws Exception{

                store=session.getStore("pop3");
                store.connect(host,username,password);

                folder=store.getFolder("INBOX");
                folder.open(Folder.READ_ONLY);

                message=folder.getMessages();
        }

        public void close()throws Exception{

                folder.close(false);
                store.close();
        }


}

TOP

6. 这是处理密码的两个类,我网上找的

import java.io.*;
import java.util.*;

/**
* This class prompts the user for a password and attempts to mask input with "*"
*/

public class PasswordField {

  /**
   *@param input stream to be used (e.g. System.in)
   *@param prompt The prompt to display to the user.
   *@return The password as entered by the user.
   */

   public static final char[] getPassword(InputStream in, String prompt) throws IOException {
      MaskingThread maskingthread = new MaskingThread(prompt);
      Thread thread = new Thread(maskingthread);
      thread.start();

      char[] lineBuffer;
      char[] buf;
      int i;

      buf = lineBuffer = new char[128];

      int room = buf.length;
      int offset = 0;
      int c;

      loop:   while (true) {
         switch (c = in.read()) {
            case -1:
            case '\n':
               break loop;

            case '\r':
               int c2 = in.read();
               if ((c2 != '\n') && (c2 != -1)) {
                  if (!(in instanceof PushbackInputStream)) {
                     in = new PushbackInputStream(in);
                  }
                  ((PushbackInputStream)in).unread(c2);
                } else {
                  break loop;
                }

                default:
                   if (--room < 0) {
                      buf = new char[offset + 128];
                      room = buf.length - offset - 1;
                      System.arraycopy(lineBuffer, 0, buf, 0, offset);
                      Arrays.fill(lineBuffer, ' ');
                      lineBuffer = buf;
                   }
                   buf[offset++] = (char) c;
                   break;
         }
      }
      maskingthread.stopMasking();
      if (offset == 0) {
         return null;
      }
      char[] ret = new char[offset];
      System.arraycopy(buf, 0, ret, 0, offset);
      Arrays.fill(buf, ' ');
      return ret;
   }
}





import java.io.*;

/**
* This class attempts to erase characters echoed to the console.
*/

class MaskingThread extends Thread {
   ** volatile boolean stop;
   ** char echochar = '*';

  /**
   *@param prompt The prompt displayed to the user
   */
   public MaskingThread(String prompt) {
      System.out.print(prompt);
   }

  /**
   * Begin masking until asked to stop.
   */
   public void run() {

      int priority = Thread.currentThread().getPriority();
      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

      try {
         stop = true;
         while(stop) {
           System.out.print("\010" + echochar);
           try {
              // attempt masking at this rate
              Thread.currentThread().sleep(1);
           }catch (InterruptedException iex) {
              Thread.currentThread().interrupt();
              return;
           }
         }
      } finally { // restore the original priority
         Thread.currentThread().setPriority(priority);
      }
   }

  /**
   * Instruct the thread to stop masking.
   */
   public void stopMasking() {
      this.stop = false;
   }
}

TOP

最后用来调用给个类的包括main()方法的类

/****************************************************************
* This class load the application
*
*****************************************************************/




import javax.mail.*;
import java.io.IOException;

class Tester{
        public static void main(String[] args){


                In in=new In();
                char flag;
                MailBox box=null;
                MailDemo mailReceive=null;




                while(true){
            //display the menus
                        System.out.println("     Welcome the pop3 email system");
                        System.out.println("      plesase choose one option:");
                        System.out.println("1...Greate a new account");
                        System.out.println("2...Choose an account and receive the pop3 email");
                        System.out.println("3...Choose an account and view the email");

                        System.out.println("X...Exit");
                        flag=in.getChar();


                                switch(flag){
                                        //create the account
                                        case '1':{
                                                System.out.println("Enter the Account Name");
                                                String name=in.getString();


                                                System.out.println("Enter the host");
                                                String host=in.getString();

                                                System.out.println("Enter the username");
                                                String user=in.getString();

                                                char password[] = null;

                                                try {
                                                                  password = PasswordField.getPassword(System.in, "Enter your password: \n");
                                                                      }
                                            catch(IOException ioe) {
                                                         ioe.printStackTrace();
              }
                                                AccountInfo info=new AccountInfo(name,host, user, String.valueOf(password));
                                                Account account=new Account(info);
                                                box=new MailBox();
                                                box.addAccount(account);
                                                box.addMail(account);
                                        }
                                        break;
                    //receive the mails
                                        case '2':{
                                                char temp=' ';
                                                int choice=0;
                                                AccountInfo[] allAccount=box.getAllAccount();
                                                for(int i=0;i<allAccount.length;i++){
                                                        System.out.println((i+1)+"..."+allAccount.getAccountName());
                                                }
                                                System.out.println("Please choose an account");
                                                temp=in.getChar();
                                                choice=(int)temp-49;


                                                System.out.println("You have chosen  "+allAccount[choice].getAccountName());
                                                mailReceive =new MailDemo(allAccount[choice]);
                                                try{
                                                        mailReceive.getMail();
                                                        Account tempAccount=box.getMail(allAccount[choice].getAccountNo());
                                                        Message[] tempMessage=mailReceive.getMessage();
                                                        Mail[] tempMail=new Mail[tempMessage.length];
                                                        for(int i=0;i<tempMail.length;i++){
                                                                tempMail=new Mail(tempMessage);
                                                        }
                                                        tempAccount.addMail(tempMail);
                                                        box.addMail(tempAccount);

                                                        System.out.println("Receive successful");


                                                }
                                                catch(Exception e){
                                                        System.err.println("Can't receive the Email");
                                                }



                                        }
                                        break;
                    //view the mails
                                        case '3':{
                                                char temp=' ';
                                                int choice=0;
                                                AccountInfo[] allAccount=box.getAllAccount();
                                                for(int i=0;i<allAccount.length;i++){
                                                        System.out.println((i+1)+"..."+allAccount.getAccountName());
                                                }
                                                System.out.println("Please choose an account");
                                                temp=in.getChar();
                                                choice=(int)temp-49;
                                                Account tempAccount=box.getMail(allAccount[choice].getAccountNo());
                                                Mail[] tempMail=tempAccount.getMail();
                                                Message[] tempMessage=new Message[tempMail.length];
                                                try{
                                                        System.out.println("This account have such email follows");
                                                        for(int i=1;i<tempMail.length;i++){

                                                                tempMessage=tempMail.getMessage();
                                                                System.out.println((i)+"..."+tempMessage.getFrom()[0].toString()+"\t"+tempMessage.getSubject().toString());

                                                        }
                                                        System.out.println();
                                                        System.out.println();
                                                        System.out.println("Select One to view the detail");
                                                        temp=in.getChar();
                                                        choice=(int)temp-48;
                                                        System.out.println(tempMessage[choice].getContent());

                                                }
                                                catch(Exception e){
                                                        System.err.println("Can't get the title");
                                                }





                                        }
                                        break;
                                        //exit the applcation
                                        case 'x':{
                                                try{
                                                        mailReceive.close();
                                                }
                                                catch(Exception e){
                                                }
                                                System.exit(0);
                                        }




                                        default:




                                }
                        }

                }
        }

TOP

发新话题