⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receivemailapplet.java

📁 Java网络编程实例第7章实例.rar
💻 JAVA
字号:
package mailapplet;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.*;import java.util.*;import javax.swing.*;public class receiveMailApplet extends Applet {  private boolean isStandalone = false;  private Label label1 = new Label();  private TextField textField1 = new TextField();  private Label label2 = new Label();  private TextField textField2 = new TextField();  private Label label3 = new Label();  private TextField textField3 = new TextField();  private TextArea textArea1 = new TextArea();  private Button button1 = new Button();  private Button button2 = new Button();  public static String MAIL_STORE = "pop3";  //Get a parameter value  public String getParameter(String key, String def) {    return isStandalone ? System.getProperty(key, def) :      (getParameter(key) != null ? getParameter(key) : def);  }  //Construct the applet  public receiveMailApplet() {  }  //Initialize the applet  public void init() {    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception {    label1.setText("邮件服务器地址");    label1.setBounds(new Rectangle(54, 25, 94, 27));    this.setLayout(null);    textField1.setBounds(new Rectangle(151, 25, 174, 28));    label2.setText("用户名");    label2.setBounds(new Rectangle(53, 61, 43, 26));    textField2.setBounds(new Rectangle(151, 63, 174, 29));    label3.setText("密码");    label3.setBounds(new Rectangle(52, 102, 33, 25));    textField3.setEchoChar('*');    textField3.setBounds(new Rectangle(151, 101, 175, 29));    textArea1.setEditable(false);    textArea1.setBounds(new Rectangle(13, 184, 551, 365));    button1.setLabel("登录");    button1.setBounds(new Rectangle(143, 142, 77, 28));    button1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button1_actionPerformed(e);      }    });    button2.setLabel("重写");    button2.setBounds(new Rectangle(237, 142, 77, 28));    button2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button2_actionPerformed(e);      }    });    this.add(label1, null);    this.add(textField1, null);    this.add(label2, null);    this.add(textField2, null);    this.add(label3, null);    this.add(textField3, null);    this.add(textArea1, null);    this.add(button1, null);    this.add(button2, null);  }  //Start the applet  public void start() {  }  //Stop the applet  public void stop() {  }  //Destroy the applet  public void destroy() {  }  //Get Applet information  public String getAppletInfo() {    return "Applet Information";  }  //Get parameter info  public String[][] getParameterInfo() {    return null;  }  //Main method  public static void main(String[] args) {    receiveMailApplet applet = new receiveMailApplet();    applet.isStandalone = true;    Frame frame;    frame = new Frame() {      protected void processWindowEvent(WindowEvent e) {        super.processWindowEvent(e);        if (e.getID() == WindowEvent.WINDOW_CLOSING) {          System.exit(0);        }      }      public synchronized void setTitle(String title) {        super.setTitle(title);        enableEvents(AWTEvent.WINDOW_EVENT_MASK);      }    };    frame.setTitle("Applet Frame");    frame.add(applet, BorderLayout.CENTER);    applet.init();    applet.start();    frame.setSize(500,620);    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);    frame.setVisible(true);  }  void button2_actionPerformed(ActionEvent e) {    textField1.setText("");    textField2.setText("");    textField3.setText("");  }  void button1_actionPerformed(ActionEvent e) {    String mail_host = textField1.getText();    String user = textField2.getText();    String pass = textField3.getText();    if ((user==null)||(user.trim().length()==0))    {      JOptionPane msg = new JOptionPane();      JOptionPane.showMessageDialog(this, "用户名不能为空!", "用户名不能为空!", 0);    }    else if ((pass==null)||(pass.trim().length()==0))    {      JOptionPane msg = new JOptionPane();      JOptionPane.showMessageDialog(this, "密码不能为空!", "密码不能为空!", 0);    }    else    {      try      {        Properties props = new Properties();        props.put("mail.host",  mail_host);        props.put("mail.transport.protocol",  "pop3");        Session receiveMailSession;        boolean  sessionDebug  =  false;        receiveMailSession = Session.getDefaultInstance(props, null);        receiveMailSession.setDebug(sessionDebug);        URLName url = new URLName(MAIL_STORE, mail_host, -1, "INBOX", user, pass);        Store store = receiveMailSession.getStore(url);        store.connect();        Folder inbox = store.getFolder("INBOX");        inbox.open(Folder.READ_WRITE);        int n = inbox.getMessageCount();        Message[] msg = inbox.getMessages();        for(int i=0;i<n;i++)        {          textArea1.append("Letter " + String.valueOf(i+1) + "\n");          textArea1.append("Date: " + msg[i].getSentDate() + "\n");          Address a[] = msg[i].getFrom();          textArea1.append("From: " + a[0] + "\n");          a = msg[i].getRecipients(Message.RecipientType.TO);          textArea1.append("To: " + a[0] + "\n");          String s = msg[i].getSubject();          textArea1.append("Subject: " + s + "\n");          Object o = msg[i].getContent();          if (msg[i].isMimeType("text/plain"))          {            textArea1.append(o + "\n");          }          else if (msg[i].isMimeType("multipart/*"))          {            Multipart mp = (Multipart)o;            for(int j=0;j<mp.getCount();j++)            {              Part part = mp.getBodyPart(j);              String contentType = part.getContentType();              if (contentType==null)              {                textArea1.append("Bad content type for part " + String.valueOf(j) + "\n");                continue;              }              ContentType ct = new ContentType(contentType);              if (j!=0)              {                textArea1.append("\n");              }              if (ct.match("text/plain"))              {                textArea1.append(part.getContent() + "\n");              }              else              {                String desc = "Attachment ";                s = part.getFileName();                if(s!=null)                {                  desc+= s;                }                textArea1.append(desc + "\n");              }            }          }        }      }      catch(Exception e1)      {        //out.println("Unable to connect to MailServer");      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -