Recieving Email on J2ME devices.docx
- 文档编号:23345519
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:12
- 大小:20KB
Recieving Email on J2ME devices.docx
《Recieving Email on J2ME devices.docx》由会员分享,可在线阅读,更多相关《Recieving Email on J2ME devices.docx(12页珍藏版)》请在冰豆网上搜索。
RecievingEmailonJ2MEdevices
ExamplebelowdemonstrateasimpleemailclientonJ2MEdevices.
/*Midlet running on device*/
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 CheckMail extends MIDlet implements CommandListener {
private String MAIL_SERVER_URL =
"http:
//localhost:
8080/examples/servlet/CheckMailServlet?
";
private int MSG_LIST_SIZE = 5;
Display display = null;
List menu = null;
List dmenu = null;
TextBox input = null;
String[] msgs = new String[MSG_LIST_SIZE];
String user = null;
int status = 0;
Command backCommand = new Command("Back", Command.BACK, 0);
Command submitCommand = new Command("Submit", Command.OK, 2);
Command exitCommand = new Command("Exit", Command.STOP, 3);
public CheckMail() { }
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
displayMenu();
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command c, Displayable d) {
if(c == exitCommand ) {
destroyApp(true);
} else if (c == backCommand) {
handleBack();
} else if (c == submitCommand) {
user = input.getString();
retreiveMail(0, null);
} else if (d == dmenu) {
handleMainMenu();
} else if (d == menu) {
handleMsgMenu();
} else
loginUser();
}
/* Display Main Screen */
private void displayMenu() {
dmenu = new List("Check Email", Choice.IMPLICIT);
if (user == null)
dmenu.append("Login", null);
else
dmenu.append("Logout", null);
dmenu.append("Check Mail", null);
dmenu.addCommand(exitCommand);
dmenu.setCommandListener(this);
display.setCurrent(dmenu);
status = 0;
}
/* This method takes users Mail ID and password*/
private void loginUser() {
input = new TextBox(
"Enter Login Name/Password (Seperate by /) :
", "", 25,
TextField.ANY);
input.addCommand(submitCommand);
input.addCommand(backCommand);
input.setCommandListener(this);
display.setCurrent(input);
}
/* This method retrieves Message with msgNum and
if no MsgNum, displays the list of all msgs*/
private void retreiveMail(int s, String msgNum) {
String popURL = MAIL_SERVER_URL + "u=" + user;
if (s == 0)
popURL = popURL // Login
if (s == 1)
// List messages
popURL = popURL + "&s=" + msgNum + "&a=l";
else if (s == 2)
// Retreive message with i = msgNum
popURL = popURL + "&i=" + msgNum + "&a=r";
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(); }
}
if (err == null) {
if (s == 0) {
user = sb.toString();
if (user.indexOf("INVALIDUSR") >= 0) {
user = null;
showAlert("Invalid User Name and Password");
} else {
input = null;
dmenu = null;
displayMenu();
}
} else if (s == 1) // For retreiving the list
showMList(sb.toString());
else // s ==2, show Message
showMsg(sb.toString());
} else
showAlert(err); // Need to Show Error Page
}
/* Display the selected Message*/
private void showMsg(String msg) {
StringBuffer buffer = new StringBuffer(200);
int delim = 'ß';
int j = msg.indexOf(delim);
int i =0;
// Get's FROM
buffer.append(msg.substring(0,j ));
for (int k=0;k<4;k++) {
i = j+1;
j = msg.indexOf(delim, i);
// Get's TO, CC, Subject, Date
buffer.append(msg.substring(i,j ));
}
// Rest of the Message
buffer.append("\n" + msg.substring(j+1 ));
showAlert(buffer.toString());
}
/* Display Error On screen*/
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
/* Display MessageList of User*/
private void showMList(String msgStr) {
for (int i=0;i msgs[i] = ""; } menu = new List("Messages", Choice.IMPLICIT); int delim = 'ß'; String sub = null; int i =0; int k = 0; int l =0; int j =msgStr.indexOf(delim); while( j >= 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() { if (status == 1) { dmenu = null; displayMenu(); } else display.setCurrent(menu); } private void handleMainMenu() { int index = dmenu.getSelectedIndex(); switch(index) { case 0 : if (user ! = null) { retreiveMail(1, "0"); break; } case 1 : status = 1; loginUser(); break; case 2 : break; } } private void handleMsgMenu() { int index = menu.getSelectedIndex(); if (! msgs[index].equals("")) { retreiveMail(2, msgs[index]); // Retreive Mail } } } /*Servlet communicating with Mail server*/ import java.io.*; import java.util.StringTokenizer; import java.util.Vector; import javax.mail.*; import javax.mail.internet.MimeMessage; import javax.servlet.*; import javax.servlet.http.*; public class CheckMailServlet extends HttpServlet { private static String MSG_NOMAIL = "NOMAIL"; private static String MSG_INVALID_USER = "INVALIDUSR"; private static String MSG_CRITCAL_ERR = "UNEXPECTEDERROR"; public CheckMailServlet() { String hostname = "" // Get system properties Properties props = System.getProperties(); props.put("mail.pop3.host", "hostname"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("pop3"); } /*Get the request parameters */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String action = request.getParameter("a"); if(action == null) doLogin(request, response); else if(action.equals("r")) //retrieve doRetrieve(request, response); else if(action.equals("l")) //List doList(request, response); } catch(Exception e) { PrintWriter writer = response.getWriter(); writer.println(MSG_CRITCAL_ERR); writer.flush(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request, response); } /* performs login for user and returns status*/ private void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException { String uid = request.getParameter("u"); String pwd, tmpUID ; String responeStr = null; if ((uid == null) || (uid.equals("")) ) { responeStr = MSG_INVALID_USER; } else { int j =uid.indexOf('/'); if (j > 0) { tmpUID = uid.substring(0,j); pwd = uid.substring(j+1); if(login(tmpUID,pwd)) { responeStr = tmpUID; flag = true; } else
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Recieving Email on J2ME devices