Hi all,
Can is send and receive mail using J2ME without using server say Apache Tomcat. And also provide information about which api must be used for mailing.
Thanks all
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi all,
Can is send and receive mail using J2ME without using server say Apache Tomcat. And also provide information about which api must be used for mailing.
Thanks all
Good morning chals,
I found this code which is a simple Email client for J2ME. It should be what you are looking for..
/*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<MAX_LIST_SIZE;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 = "yourserver.com" // 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 responeStr = MSG_INVALID_USER; } else responeStr = MSG_INVALID_USER; } PrintWriter writer = response.getWriter(); writer.print(responeStr); writer.flush(); } public boolean login(String uid, String pwd) // login { store.connect(hostname,uid,pwd); if (store.isConnected()) { return true; } else return false; } /* List all messages from Inbox*/ private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException { String uid = request.getParameter("u"); String responeStr = ""; MailLister lister = null; StringBuffer buffer = new StringBuffer(200); try { lister = getMailLister(uid); } catch(Exception exp) { throw new ServletException(exp.getMessage()); } if ( (lister != null) && (lister.size() > 0)) { Vector vect = lister.keys(); int listSize = vect.size(); int endingNum = (listSize > 8) ? (listSize - 8) : 0; for (int j = listSize-1; j>=endingNum; j--) { String curKey = (String)vect.elementAt(j); String msgString = (String)lister.get(curKey); if (j != endingNum) buffer.append( curKey + "ß" + msgString + "ß"); else buffer.append(curKey + "ß" + msgString); } responeStr = buffer.toString(); } else responeStr = MSG_NOMAIL; PrintWriter writer = response.getWriter(); writer.print(responeStr); writer.flush(); } /* Stores all messages in Hashtable */ private MailLister getMailLister(String uid) throws Exception { MailLister lister = null; int j =uid.indexOf('/'); String ud; String pwd; if (j > 0) { ud = uid.substring(0,j); pwd = uid.substring(j+1); if(login(ud,pwd)) { // Open the Folder Folder folder = store.getDefaultFolder(); if (folder == null) { System.out.println("No default folder"); return null; } folder = folder.getFolder("INBOX"); if (folder == null) { System.out.println("Invalid folder"); return null; } folder.open(Folder.READ_ONLY); // Get directory Message message[] = folder.getMessages(); lister = new MailLister() for (int i=0, n=message.length; i<n; i++) { Message m = folder.getMessage(i); lister.put(m,i); } } else return lister; } else return lister; return lister; } /* Retrieve Selected Mail from Inbox with msgID*/ private void doRetrieve(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException { String msgId = request.getParameter("i"); String uid = request.getParameter("u"); try { Message msg = getMessageFromStore(msgId, uid); if (msg == null) throw new ServletException( "Could not Load the Message File"); writeMessage(msg, request, response, uid, msgId); } catch(Exception exp) { throw new ServletException(exp.getMessage()); } } private String getMsgHeading(Message msg) throws Exception { StringBuffer buffer = new StringBuffer(300); buffer.append("From:"); getAddressesList(msg.getFrom(), buffer); buffer.append(" To:"); getAddressesList( msg.getRecipients(javax.mail.Message.RecipientType.TO), buffer); buffer.append(" Subject:"); buffer.append(msg.getSubject()); buffer.append(" Date:"); buffer.append(msg.getSentDate()); return buffer.toString(); } /* This method gives the name and email of person*/ private void getAddressesList(Address addresses[], StringBuffer buffer) throws Exception { String uName; String uEmail; if(addresses != null) { for(int i = 0; i < addresses.length; i++) { buffer.append(addresses[i] + ";"); } } } /* write Message according to MIME type and send to mobile screen*/ protected void writeMessage(Message msg, HttpServletRequest request, HttpServletResponse response, String uid, String msgId) throws Exception { if(msg.isMimeType("multipart/*")) { Multipart multipart = (Multipart)msg.getContent(); int msgPartCount = multipart.getCount(); StringBuffer buffer = new StringBuffer(200); for(int i = 0; i < msgPartCount; i++) { Part p = multipart.getBodyPart(i); if (i == 0) { buffer.append(getMsgHeading(msg)); } if(p.isMimeType("text/plain")) { String content = (String)p.getContent(); buffer.append(content.trim()); } else { String filename = p.getFileName(); buffer.append(" --- Has attachement " + filename); } if (i == (msgPartCount-1)) { response.setContentType("text/plain"); PrintWriter writer = null; writer = response.getWriter(); writer.print(removeExtraChar(buffer.toString(), '\n')); writer.flush(); } } } else if(msg.isMimeType("text/plain")) { printAscii(response, (String)msg.getContent() , msg); } else // For HTML or another type of Content { writePart(msg, -1, response); } } private void printAscii(HttpServletResponse response, String currentText, Message msg ) throws Exception { PrintWriter writer = response.getWriter(); if (currentText.length() > 1500) currentText = currentText.substring(0,1500); writer.print(getMsgHeading(msg) + currentText); writer.flush(); } private Message getMessageFromStore(String msgUID, String uid) throws Exception { MailLister lister = getMailLister(uid) if(lister != null) { return lister.get(msgUID); } else return null; } private void writePart(Message msg, int partnr, HttpServletResponse response) throws Exception { Part p = msg; String responseStr ; if (p.isMimeType("text/html")) { String str = (String)p.getContent(); if (str.length() > 1500) str = str.substring(0,1500); responseStr = removeHTMLFormatting(str); responseStr = removeExtraChar(responseStr, '\n'); } else responseStr = "The MIME Type in which this mail exist is not"+ " supported on J2ME Phone"; response.setContentType("text/plain"); PrintWriter writer = response.getWriter(); writer.print(getMsgHeading(msg) + responseStr); writer.flush(); } private String removeHTMLFormatting(String str) { boolean insideTag = false; String token; StringBuffer buffer = new StringBuffer(200); StringTokenizer st = new StringTokenizer( str , "<>", true); while( st.hasMoreTokens() ) { token = st.nextToken(); if ( token.equals( "<" ) ) insideTag = true; else if ( token.equals( ">" ) ) insideTag = false; else if ( insideTag == false ) buffer.append( token ); } return buffer.toString(); } private String removeExtraChar(String str, int delim) { StringBuffer buffer = new StringBuffer(200); // First substring int j =str.indexOf(delim); int i = 0; while( j >= 0) { // Add some extra space buffer.append(" " + str.substring(i,j)); i = j + 1; j = str.indexOf(delim, i); // Rest of substrings } buffer.append(str.substring(i)); // Last substring return buffer.toString(); } /* This class stores all messages in Hashtable and retrieves them according to given key*/ public class MailLister implements java.io.Serializable { private Hashtable hash; private Vector vect; public MailLister() { vect = new Vector(); hash = new Hashtable(vect.capacity()); } public Object get(Object obj) { return hash.get(obj); } public void put(Object key, Object val) { if(!vect.contains(key)) vect.insertElementAt(key, vect.size()); hash.put(key, val); } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
With little change the code works fine.
But my intent is without using J2EE as middle tire (here Servlet used), can i send and receive mail. If possible, then which api i need to use.
Thanks for ur fast response.
I have no experience with this chals so I can't personally offer any more advice.
Does the JavaMail API help?
JavaMail API
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Can i user web services for j2me mail?
Yes there is a J2ME web services API.
Take a look at the Java ME SDK 3.0. I believe it's included.
Sun Java Wireless Toolkit for CLDC (formerly known as Java 2 Platform, Micro Edition (J2ME) Wireless Toolkit)
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.