www.pudn.com > javagalaxy1POPMail.zip > 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 = 'ß';
int j = msg.indexOf(delim);
int i =0;
buffer.append(msg.substring(0,j )); // Get's FROM
for (int k=0;k<4;k++) // Make 4 rounds
{
i = j+1;
j = msg.indexOf(delim, i);
buffer.append(msg.substring(i,j )); // Get's TO, CC, Subject, Date
}
buffer.append("\n" + msg.substring(j+1 )); // Rest of the Message
showAlert(buffer.toString()); */
showAlert(msg);
}
private void showAlert(String err) {
Alert a = new Alert("Error");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
private void showMList(String msgStr)
{
for (int i=0;i= 0) {
sub = msgStr.substring(i,j);
if ((k % 2) == 0) {
msgs[l] = sub; // First would be Message Number
l++;
}
else
menu.append(sub, null);
k++;
i = j + 1;
j = msgStr.indexOf(delim, i); // Rest of substrings
}
sub = msgStr.substring(i); // Last substring
menu.append(sub, null);
menu.addCommand(backCommand);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
status = 1;
display.setCurrent(menu);
}
private void handleBack() {
//System.out.println("Status == " + status);
if (status == 1) {
dmenu = null;
defaultMenu();
}
else
display.setCurrent(menu);
}
private void handleDMenu() {
int index = dmenu.getSelectedIndex();
switch(index)
{
case 0 : if (user != null) {
retreiveMail(1, "0");
break;
} // If user == null, then it will go to next line.
// So just change case numbers in case list order is changed
case 1 : status = 1; loginUsr(); break; // login
case 2 : showAlert("Implemented in Next Version"); break;
}
}
private void handleMMenu() {
int index = menu.getSelectedIndex();
if (!msgs[index].equals(""))
{
retreiveMail(2, msgs[index]); // Retreive Mail
}
else
showAlert("User selected for Immediate update"); // Provide option for Update Now
}
}