java 浏览器2

package com.email;

import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JList;
import java.util.*;
import java.awt.BorderLayout;
import javax.swing.JProgressBar;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.event.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * <p>Title: 使用JavaMail接收邮件</p>
 * <p>Description: 实例JavaMail包接收邮件,本实例没有实现接收邮件的附件。</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Filename: POPMail.java</p>
 * @version 1.0
 */
public class POPMail extends javax.swing.JDialog implements  WindowListener {
/**
 *<br>方法说明:主方法,接收用户输入的邮箱服务器、用户名和密码
 *<br>输入参数:
 *<br>返回类型:
 */
 String server,user,pass,mailfrom;
 PropertyFileOperation op;
 private JLabel jLabel;
 private JList jList;
 private JProgressBar jProgressBar;
 private int sum,mailNum;
private Vector v=new Vector();
POPMail inst;
 public void windowOpened(WindowEvent windowEvent) {
  TMail doIt = new TMail();
  doIt.start();
 }
 public class TMail extends Thread {
     public void run(){
      getMail();
     }
 }
   public void windowClosed(WindowEvent windowEvent) {  }
   public void windowIconified(WindowEvent windowEvent) {  }
   public void windowDeiconified(WindowEvent windowEvent) {  }
   public void windowActivated(WindowEvent windowEvent) {  }
   public void windowDeactivated(WindowEvent windowEvent) { }
   public void windowClosing(WindowEvent windowEvent) {
      System.exit(0);
    }
 //  private void center() {
   // Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
   // setLocation( ( ( screenDim.width - getSize().width ) / 2 ),
   // ( ( screenDim.height - getSize().height ) / 2) );
   // }


 /**
 * Auto-generated main method to display this JDialog
 */
 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    POPMail inst = new POPMail();
    inst.setLocationRelativeTo(null);   
    
    inst.setVisible(true);
    
    
   }
  });
  //getMail();
 }
 public POPMail(int init)
 {
   inst = new POPMail();
  inst.setLocationRelativeTo(null);
  inst.setModal(true);
  inst.setResizable(false);
  inst.setVisible(true);
  inst.pack();
  
  
  //Thread.currentThread().sleep(2000);
 }
 public POPMail() {
  super();
  this.addWindowListener(this);
  initGUI();
  ///getMail();
 }
 
public void getMail()
{
 
///
 op=new PropertyFileOperation("MailServer.properties");//读取邮箱配置
 
 v.addElement("加载邮箱配置");
 this.jList.setListData(v);
重邮箱更新粮库服务器信息/
 
 int num=Integer.parseInt(op.getVal("num"));
 server=op.getVal("server");
   user=op.getVal("user");
   pass=op.getVal("pass");
   mailNum=num;
   mailfrom=op.getVal("from");
  
 //System.out.println(num);
   v.addElement("加载服务器配置");
  this.jList.setListData(v);
  
   op=new PropertyFileOperation("GrainStoreServer.properties");
   v.addElement("更新服务器信息");
  this.jList.setListData(v);
   receive(server, user, pass);
}
 
 private void initGUI() {
  try {
   {
    jProgressBar = new JProgressBar();
    getContentPane().add(jProgressBar, BorderLayout.SOUTH);
    this.jProgressBar.setMaximum(100);
    this.jProgressBar.setMinimum(0);
   }
   {
    jLabel = new JLabel();
    getContentPane().add(jLabel, BorderLayout.NORTH);
    jLabel.setText("\u670d\u52a1\u5668\u4fe1\u606f\u66f4\u65b0\u4e2d\uff0c\u8bf7\u7a0d\u540e...");
    jLabel.setPreferredSize(new java.awt.Dimension(392, 36));
   }
   {
    ListModel jListModel =
     new DefaultComboBoxModel(
       new String[] { "" });
    jList = new JList();
    getContentPane().add(jList, BorderLayout.CENTER);
    jList.setModel(jListModel);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jList.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
   }
   this.setSize(400, 263);
  } catch (Exception e) {
   //e.printStackTrace();
   v.addElement("错误信息:"+e.getMessage());
          this.jList.setListData(v);
  }
 }
 /*public POPMail(String popserver,String popuser,String poppass,int num,String popfrom)
 {
  //SwingUtilities.invokeLater(new Runnable() {
   //public void run() {
    //POPMail inst = new POPMail();
    //inst.setModal(true);
    //inst.setVisible(true);
   //}
  //});
  POPMail inst = new POPMail();
  //inst.setModal(true);
  inst.setVisible(true);
  initGUI();
   
    op=new PropertyFileOperation("GrainStoreServer.properties");
    server=popserver;
    user=popuser;
    pass=poppass;
    mailNum=num;
    mailfrom=popfrom;
    receive(popserver, popuser, poppass);
    inst.pack();
 }*/

/**
 *<br>方法说明:接收邮件信息
 *<br>输入参数:
 *<br>返回类型:
 */
    public  void receive(String popServer, String popUser, String popPassword){
        Store store=null;
        Folder folder=null;
        try{
            //获取默认会话
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
            //使用POP3会话机制,连接服务器
            store = session.getStore("pop3");
            store.connect(popServer, -1,popUser,popPassword);
            //store.connect(popServer, popUser, popPassword);
            //获取默认文件夹
            folder = store.getDefaultFolder();
            if (folder == null) throw new Exception("无默认文件夹");
            //如果是收件箱
            folder = folder.getFolder("INBOX");
            if (folder == null) throw new Exception("无服务器信息");
            //使用只读方式打开收件箱
            folder.open(Folder.READ_ONLY);
            //得到文件夹信息,获取邮件列表
            Message[] msgs = folder.getMessages();
         
           
            int mailCount=msgs.length;
            int msgNum=0;
           
             for (msgNum = 0; msgNum < mailCount; msgNum++){
             //if(msgNum==mailNum)//如果检索的结果等于服务器的数目,跳出循环
             // break;
             int percent =(int)((msgNum+1)*100/mailCount);
             //System.out.println(percent);
           
             //this.jTextField.setText(Integer.toString(percent));
             //this.jTextField.repaint();
             //this.jTextField.validate();
             this.jProgressBar.setValue(percent);
             this.setTitle("服务器列表更新:"+Integer.toString(percent)+"%");
             jProgressBar.repaint();
             jProgressBar.validate();
                //frame.setVisible(true);
                //Thread.sleep(500);
             
            saveMsg(msgs[msgNum]);
            
            }
           
             //Dimension   screensize=Toolkit.getDefaultToolkit().getScreenSize();    
             //this.getParent().setLocation(0, 0);
             //this.getParent().setSize(screensize.width,screensize.height-20);
            
            
        }catch (Exception ex){
            //ex.printStackTrace();
         v.addElement("错误信息:"+ex.getMessage());
          this.jList.setListData(v);
        }
        finally{
        //释放资源
            try{
                if (folder!=null) folder.close(false);
                if (store!=null) store.close();
               
               
            }catch (Exception ex2) {
               // ex2.printStackTrace();
             v.addElement("错误信息:"+ex2.getMessage());
              this.jList.setListData(v);
            }
            finally
            {
             this.dispose();
                inst=null;
            }
        }
    }


/**
 *<br>方法说明:打印邮件信息
 *<br>输入参数:Message message 信息对象
 *<br>返回类型:
 */
    public  void saveMsg(Message message){
        try{
            //获得发送邮件地址
            String from=((InternetAddress)message.getFrom()[0]).getPersonal();
            if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();
           // System.out.println("FROM: "+from);
           
            if(from.equals(mailfrom) || mailfrom.equals(""))
             {
            //获取主题
            String subject=message.getSubject();
            //System.out.println("SUBJECT: "+subject);
         String[] str = subject.split("\\|");
         if(str.length==3)
         {
          v.addElement("更新服务器"+str[0]+"配置");
          this.jList.setListData(v);
            op.setProperties("server"+str[0], str[1]);
            op.setProperties("port"+str[0], str[2]);
           // op.setProperties("flag"+str[0], "1");
          //  sum+=1;
           // System.out.println("server"+str[0]+":"+str[1]);
           // System.out.println("port"+str[0]+":"+str[2]);
           // System.out.println(subject);
         }
             }
           
            //获取信息对象
           // Part messagePart=message;
            //Object content=messagePart.getContent();
            //附件
            //if (content instanceof Multipart){
              //  messagePart=((Multipart)content).getBodyPart(0);
               // System.out.println("[ Multipart Message ]");
           // }
            //获取content类型
            //String contentType=messagePart.getContentType();
            //如果邮件内容是纯文本或者是HTML,那么打印出信息
            //System.out.println("CONTENT:"+contentType);
           // if (contentType.startsWith("text/plain")||
                //contentType.startsWith("text/html")){
               // InputStream is = messagePart.getInputStream();
               // BufferedReader reader=new BufferedReader(new InputStreamReader(is));
               // String thisLine=reader.readLine();
               // while (thisLine!=null){
                    //System.out.println(thisLine);
                    //thisLine=reader.readLine();
               // }
            //}
            //System.out.println("-------------- END ---------------");
        }catch (Exception ex){
            //ex.printStackTrace();
            v.addElement("错误信息:"+ex.getMessage());
          this.jList.setListData(v);
        }
    }
}

 

 

package com.email;

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class PropertyFileOperation  {
 
 private File file;
 private String filename;
 private Properties p = new Properties();
public PropertyFileOperation(String fileName)
{
 file = new File (System.getProperty("user.dir")+"\\INF\\"+fileName);
 filename=System.getProperty("user.dir")+"\\INF\\"+fileName;
 getProperties();
}
    public void getProperties(){

        //ClassLoader cl = this.getClass().getClassLoader();
        FileInputStream input;
        // if(cl!=null)
        //input=cl.getResourceAsStream(filename);
        if(!file.exists())
        {
         //System.out.println(filename);
         JOptionPane.showMessageDialog(null,  "配置文件不存在","提示", JOptionPane.ERROR_MESSAGE);  
         return;
        }
        try
        {
         input = new FileInputStream(filename);
         p.load(input);
        }
        catch(IOException e)
        {
         JOptionPane.showMessageDialog(null,  "配置文件加载错误","提示", JOptionPane.ERROR_MESSAGE);
         e.printStackTrace();
        }
        //else
        //input=ClassLoader.getSystemResourceAsStream(filename);


    }

 

 public void setProperties(String field,String val) {
        p.setProperty(field,val);
        try {
              OutputStream fos = new FileOutputStream(file);
              p.store(fos, filename);
              fos.close();
        } catch (FileNotFoundException ex) {
            //System.out.println("file is NULL !!!");
              ex.printStackTrace();
        } catch (IOException ex) {
           // System.out.println("IO is Error !!!");
              ex.printStackTrace();
        }


 }
 public String getVal(String field)
 {
  return p.getProperty(field);
 }
 
}

 

 

 

附:配置文件

GrainStoreServer.properties

#C:\Documents and Settings\admin\workspace\JExplorer\INF\GrainStoreServer.properties
#Thu Sep 20 14:11:31 CST 2007
server04=cn.msn.com
server03=cn.yahoo.com.cn
port04=80
server02=www.sina.com
port03=80
server01=www.baidu.com
port02=80
port01=80

 

MailServer.properties

#C:\Documents and Settings\admin\workspace\JExplorer\INF\MailServer.properties
#Tue Sep 18 17:09:10 CST 2007
port=110
server=pop3.163.com
user=user@163.com
num=50
from=
pass=pass

Tree.properties

server1=[01]±±¾©
server2=[02]ÇàÔÆ
server3=[03]²ýƽ
server4=[04]Î÷³Ç



 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值