📄 checkpopmail.java
字号:
/* ********************************************************** Title: J2ME POP3 Email Retreival * Version: 1.0 * Copyright: Copyright (c) 1999 -2000 * Author: Sudhir * Email: sudhir@javacommerce.com * URL : http://www.nextel.com/ * ********************************************************** */import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import javax.microedition.io.*;import java.io.InputStream;import java.io.PrintStream;public class CheckPopMail extends MIDlet implements CommandListener { private String MAIL_SERVER_URL = "http://168.73.26.198:156/servlet/J2MePop3?"; private int MAX_LIST_SIZE = 10; //At any time max 10 messages will be shown Display display = null; List menu = null; List dmenu = null; TextBox input = null; String[] msgs = new String[MAX_LIST_SIZE]; String user = null; int status = 0; // 0 : Very starting screen // 1 : Showing Email Subjects List static final Command backCommand = new Command("Back", Command.BACK, 0); static final Command submitCommand = new Command("Submit", Command.OK, 2); static final Command exitCommand = new Command("Exit", Command.STOP, 3); public CheckPopMail() { } public void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); defaultMenu(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); } // handle events. public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if(label.equals("Exit")) { destroyApp(true); } else if (label.equals("Back")) { handleBack(); } else if (label.equals("Submit")) { user = input.getString(); retreiveMail(0, null); } else if (d == dmenu) { handleDMenu(); // Handle Default Menu } else if (d == menu) { handleMMenu(); // Handle Main Menu which has list of Messages } else loginUsr(); } private void defaultMenu() { dmenu = new List("POP3 Email", Choice.IMPLICIT); //2, Change Order dmenu.append("Check Email", null); //2, Change Order if (user == null) dmenu.append("Login", null); //1 else dmenu.append("Logout", null); //1 // If user already logged in, show Logout dmenu.append("Send Mail", null); //3 dmenu.addCommand(exitCommand); dmenu.setCommandListener(this); display.setCurrent(dmenu); status = 0; } //Prompt the user to input his/her name. private void loginUsr() { input = new TextBox("Enter Login Name/Password (Seperate by /) :", "test/test", 25, TextField.ANY); input.addCommand(submitCommand); input.addCommand(backCommand); input.setCommandListener(this); input.setString(""); display.setCurrent(input); } /* param s can have the following values, 0 = User is trying to Login 1 = Retreive Subject List 2 = Retreive a particular message */ private void retreiveMail(int s, String msgNum) { // This menu can be easily constructed. It's more costly to // keep it initialized // remove only after user log's in //dmenu = null; String popURL = MAIL_SERVER_URL + "u=" + user; if (s == 1) popURL = popURL + "&s=" + msgNum + "&a=l"; // List messages starting from msgNum else if (s == 2) popURL = popURL + "&i=" + msgNum + "&a=r"; // retreive message with id = msgNum //System.out.println("Connecting to URL " + popURL); StreamConnection c = null; InputStream is=null; StringBuffer sb = null; String err = null; try { c = (StreamConnection)Connector.open(popURL, Connector.READ_WRITE); is = c.openInputStream(); int ch; sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb.append((char)ch); } } catch(Exception ex){ err = ex.getMessage(); } finally { try { if(is!= null) {is.close(); } if(c != null) {c.close(); } } catch(Exception exp) { err = exp.getMessage(); } } //System.out.print(sb.toString()); if (err == null) { if (s == 0) { user = sb.toString(); if (user.indexOf("INVALIDUSR") >= 0) { //if ((user.length() >= 10) && (user.substring(0,10).equals("INVALIDUSR"))) { //System.out.print("Matches with invalid user length"); user = null; showAlert("Invalid User Name and Password"); } else { // Until user log's out and tries to login again, // this object can be removed input = null; dmenu = null; defaultMenu(); } } else if (s == 1) // For retreiving the list showMList(sb.toString()); else // s ==2, show Message showMsg(sb.toString()); // Keep message limited to first 2000 characters } else showAlert(err); // Need to Show Error Page } private void showMsg(String msg) { StringBuffer buffer = new StringBuffer(200); // Enable this part when the new line character works good. /*int delim = '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -