这是我的图形界面,我想在里面加入smtp和pop3的功能,但是不会的啊。不知道表没表达清楚,就是我启动WebBrowser类的时候可以接收邮件。我是菜鸟,外加笨蛋的,希望高人详细的给写下。下面给出了WebBrowser相关的类,pop3和smtp类。
虽然分少,但是是我的全部了。
因为帖子内容过长,我分开贴了(不能用javamail的)
package browser;import java.awt.*;
import javax.swing.*;public class MyWebBrowser
    extends   JFrame
implements Runnable
{
/**
 * 
 */
private static final long serialVersionUID = 1L; private static final String TITLE = "MyWebBrowser"; private static final String DEF_HOME_URL
    ="http://java.sun.com/javase/ja/6/docs/ja/index.html";

private static final int DEF_WIDTH = 800;
private static final int DEF_HEIGHT = 600;  public static void main(String[] args)
{
            MyWebBrowser browser = new MyWebBrowser();
    SwingUtilities.invokeLater(browser);
  }
public void run()
{
    ControlPanel ctlPanel = new ControlPanel();
    ctlPanel.setup();
   
    MainPanel mainPanel = new MainPanel(DEF_HOME_URL);
    mainPanel.setup();     ctlPanel.setMainPanel(mainPanel);     Container baseContainer = getContentPane();
    baseContainer.setLayout(new BorderLayout());
    baseContainer.add(ctlPanel,  BorderLayout.NORTH);
    baseContainer.add(mainPanel, BorderLayout.CENTER);      setTitle(TITLE);
    setSize(DEF_WIDTH, DEF_HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
}public class HTMLViewer
extends  JPanel
implements HyperlinkListener
{
private static final String BEGIN_ERR_MSG = "<h1>";
private static final String DOES_NOT_SHOW = "を表示できません.<br>[理由]";
private static final String END_ERR_MSG = "</h1>";

private static final String WHITE_SPACE = " ";

private MainPanel mainPanel = null;
private JEditorPane htmlPane = null;
private JLabel statusLine = null;
private PageHistory history = null;

HTMLViewer(MainPanel mainPanel){
this.mainPanel = mainPanel;

EditorKit edkit = JEditorPane.createEditorKitForContentType("text/html");
this.htmlPane = new JEditorPane();
this.htmlPane.setEditorKit(edkit);
this.htmlPane.setEditable(false);
this.htmlPane.addHyperlinkListener(this);

this.statusLine = new JLabel(WHITE_SPACE);
this.history = new PageHistory();

setLayout(new BorderLayout());
add(new JScrollPane(this.htmlPane),BorderLayout.CENTER);
add(this.statusLine,BorderLayout.SOUTH);
} public void setPage(String strURL){
setPage(strURL, true);
}
void backPage(){
String strURL = this.history.back();
setPage(strURL, false);
}
void nextPage(){
String strURL = this.history.next();
setPage(strURL, false);
    }
public void hyperlinkUpdate(HyperlinkEvent ev){
HyperlinkEvent.EventType evType = ev.getEventType();
if (evType == HyperlinkEvent.EventType.ACTIVATED){
hyperlinkActivated(ev);
}
else if (evType == HyperlinkEvent.EventType.ENTERED){
hyperlinkEntered(ev);
}
else if (evType == HyperlinkEvent.EventType.EXITED){
hyperlinkExited(ev);
}
}

public void hyperlinkActivated(HyperlinkEvent ev){
if (ev instanceof HTMLFrameHyperlinkEvent){
HTMLFrameHyperlinkEvent flEv = (HTMLFrameHyperlinkEvent)ev;
HTMLDocument doc = (HTMLDocument)this.htmlPane.getDocument();
doc.processHTMLFrameHyperlinkEvent(flEv);
}
else{
String strURL = ev.getURL().toString();
setPage(strURL, true);
}
}
public void hyperlinkEntered(HyperlinkEvent ev){
String strURL = ev.getURL().toString();
this.statusLine.setText(strURL);
}
public void hyperlinkExited(HyperlinkEvent ev){
this.statusLine.setText(WHITE_SPACE);
}
private void setPage(String strURL, boolean isAddedToHistory){
try{
this.htmlPane.setPage(strURL);
this.mainPanel.setCurrentTabTitle(strURL);
if (isAddedToHistory){
this.history.add(strURL);
}
}
catch (Throwable th){
String errMsg = th.getMessage();
this.htmlPane.setText(
BEGIN_ERR_MSG + strURL + DOES_NOT_SHOW + errMsg + END_ERR_MSG
);
}
}
}

解决方案 »

  1.   

    package browser;import java.util.*;public class PageHistory 
    {
    private ArrayList<String>history = null;
    private int current = -1;

    PageHistory()
    {
    this.history = new ArrayList<String>();
    this.current = -1;
    }

    void add(String strURL)
    {
    int size = this.history.size();
    for (int index = size - 1;index > this.current; index--){
    this.history.remove(index);
    }
    this.history.add(strURL);
    this.current++;
    dbgPrintHistory();
    }

    String next()
    {
    String strURL = null;
    int size = this.history.size();
    if (size > 0){
    if (size > this.current + 1){
    this.current++;
    }
    strURL = this.history.get(this.current);
    }
    dbgPrintHistory();
    return strURL;
    }
    String back()
    {
    String strURL = null;
    int size = this.history.size();
    if (size > 0){
    if (this.current > 0){
    this.current--;
    }
    strURL = this.history.get(this.current);
    }
    dbgPrintHistory();
    return strURL;
    }
    private void dbgPrintHistory()
    {
    int size = this.history.size();

    for (int i = 0;i < size;i++){
    String strURL = this.history.get(i);
    if (i == this.current){
    System.out.println(" -> "+ strURL);
    }
    else {
    System.out.println("" + strURL);
    }
    }
    System.out.println();
    }
    } public class ControlPanel
         extends     JPanel
         implements  ActionListener
    {
         private static final String STR_BACK   = "戻る";
         private static final String STR_NEXT   = "進む";
         private static final String STR_NEW    = "タブ作成"; 
         private static final String STR_DELETE = "タブ削除";     private static final int DEF_WIDTH = 40;     private MainPanel mainPanel = null;
         private JTextField urlFld = null;     void setup()
         {
    setLayout(new FlowLayout()); JButton backBtn = new JButton (STR_BACK);
    backBtn.addActionListener(this);
    add(backBtn);

    JButton nextBtn = new JButton (STR_NEXT);
    nextBtn.addActionListener(this);
    add(nextBtn);  this.urlFld = new JTextField(DEF_WIDTH);
    this.urlFld.addActionListener(this);
    add(this.urlFld); JButton newBtn = new JButton (STR_NEW);
    newBtn.addActionListener(this);
    add(newBtn);
     
    JButton delBtn = new JButton (STR_DELETE);
    delBtn.addActionListener(this);
    add(delBtn);
          }
         
          void setMainPanel(MainPanel mainPanel)
          {
              this.mainPanel = mainPanel;
          }
          
          public void actionPerformed(ActionEvent ev)
          {
           Object srcObj = ev.getSource();
      if (srcObj instanceof JTextField) {
          actURLFieldChanged();
              }
      else {
          String actCmd = ev.getActionCommand();
                  actButtonPressed(actCmd);
              }
           }       private void actURLFieldChanged()
    {
       String strURL = this.urlFld.getText();
       this.mainPanel.setPage(strURL);
            }       private void actButtonPressed(String command)
    {
       if (command.equals(STR_BACK) ) {
           this.mainPanel.backpage();
       }
       else if (command.equals(STR_NEXT)) {
           this.mainPanel.nextPage();
       }
       else if (command.equals(STR_NEW)) {
           String strURL = this.urlFld.getText();
           this.mainPanel.newTab(strURL);
       }
       else if (command.equals(STR_DELETE)) {
           this.mainPanel.deleteTab();
       }
             }
     }package browser;import java.awt.*;
    import javax.swing.*;public class MainPanel
        extends     JPanel
    {
    private String homeURL = null;
    private JTabbedPane tabPane = null;

    MainPanel(String homeURL)
    {
    this.homeURL = homeURL;
    }
    void setup()
    {
    this.tabPane = new JTabbedPane();
    this.tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    setLayout(new BorderLayout());
    add(this.tabPane,BorderLayout.CENTER);

    newTab(this.homeURL);
    }
    void setPage(String strURL)
    {
    HTMLViewer viewer = (HTMLViewer)this.tabPane.getSelectedComponent();
    viewer.setPage(strURL);
    }
    void backpage()
    {
    HTMLViewer viewer = (HTMLViewer)this.tabPane.getSelectedComponent();
    viewer.backPage();
    }
    void nextPage()
    {
    HTMLViewer viewer = (HTMLViewer)this.tabPane.getSelectedComponent();
    viewer.nextPage();
    }
    void newTab(String strURL)
    {
    HTMLViewer viewer = new HTMLViewer(this);
    this.tabPane.add(strURL, viewer);
    int index = this.tabPane.getTabCount();
    this.tabPane.setSelectedIndex(index - 1);
    setPage(strURL);
    }
    void deleteTab()
    {
    int tabCount = this.tabPane.getTabCount();
    if (tabCount > 1){
    HTMLViewer viewer = (HTMLViewer)this.tabPane.getSelectedComponent();
    this.tabPane.remove(viewer);
    }
    }
    void setCurrentTabTitle(String strURL)
    {
    int index = this.tabPane.getSelectedIndex();
    this.tabPane.setTitleAt(index,strURL);
    }
    }
      

  2.   

    pop3的import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;class POP3Demo {
       private static String POP3Server = "pop.126.com";
        private static String USERNAME = "evelyn_vffle";
        private static String PASSWORD = "327888854";
        public static void main(String[] args) {
            int POP3Port = 110;
            Socket client = null;
            try {
             
                client = new Socket(POP3Demo.POP3Server, POP3Port);
                
                InputStream is = client.getInputStream();
                BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
               
                OutputStream os = client.getOutputStream();
                PrintWriter sockout = new PrintWriter(os, true);
                
                System.out.println("S:" + sockin.readLine());
                sockout.println("user " + POP3Demo.USERNAME);
                System.out.println("S:" + sockin.readLine());
                sockout.println("pass " + POP3Demo.PASSWORD);
                System.out.println("S:" + sockin.readLine());
                sockout.println("stat");
                String temp[] = sockin.readLine().split(" ");
                int count = Integer.parseInt(temp[1]);
                for (int i = 1; i < count + 1; i++) {
                    sockout.println("retr " + i);
                    System.out.println("埲壓偼" + i + "儊乕儖乕偺撪梕");
                    while (true) {
                        String reply = sockin.readLine();
                        System.out.println(reply);
                        if (reply.toLowerCase().equals(".")) {
                            break;
                        }
                    }
                }         } catch (IOException e) {
                System.out.println(e.toString());
            } finally {
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {}
            }
        }

    smtp的import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;class SMTPDemo {
      
        private static String sender = "evelyn_vffle";
        private static String receiver = "evelyn_vffle";
        private static String SMTPServer = "smtp.qq.com";
        public static void main(String[] args) {
            int SMTPPort = 25;
            Socket client = null;
            try {
               
                client = new Socket(SMTPDemo.SMTPServer, SMTPPort);
             
                InputStream is = client.getInputStream();
                BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
               
                OutputStream os = client.getOutputStream();
                PrintWriter sockout = new PrintWriter(os, true);
              
                System.out.println("S:" + sockin.readLine());
                sockout.println("helo");
                System.out.println("S:" + sockin.readLine());
                sockout.println("mail from: " + "<" + SMTPDemo.sender + ">");
                System.out.println("S:" + sockin.readLine());
                sockout.println("rcpt to: " + "<" + SMTPDemo.receiver + ">");
                System.out.println("S:" + sockin.readLine());
                sockout.println("data");
          
                sockout.println("subject: 偙傫偵偪偼");
           
                sockout.println("ni hao");
                sockout.println("wo shi li jian");
              
                sockout.println(".");
                sockout.println("rset");
                sockout.println("quit");
            } catch (IOException e) {
                System.out.println(e.toString());
            } finally {
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {}
            }
        }
    }
      

  3.   

    http://www.itcast.cn/itcast_static/javamailVideo.htm
    这是传智博客上mail的视频,免费下载观看,感觉不错,不防看看。