When you send mail, you connect to a server that supports the Simple Mail Transfer Protocol (SMTP). A MIDLet connects to the server through HTTP protocol interface. Send Email facility is coded in a servlet hosted on web server. Midlet send and recieve data through servlet.
private void displayMenu() { dmenu = new List("Send Email", Choice.IMPLICIT); if (user == null) dmenu.append("Login", null); else dmenu.append("Logout", null); dmenu.append("Send Mail", null); dmenu.addCommand(exitCommand); dmenu.setCommandListener(this); display.setCurrent(dmenu); status = 0; }
/* This method ask user for his email id and password*/ private void loginUser() { input = new TextBox( "Enter Login Name/Password (Seperate by /) :", "", 25, TextField.ANY);
try { c = (StreamConnection)Connector.open(url, 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) { user = sb.toString();
if (user.indexOf("INVALIDUSR") >= 0) { user = null; showAlert("Invalid User Name and Password"); } else { input = null; dmenu = null; displayMenu(); } } else showAlert(err); // Need to Show Error Page }
private void showAlert(String err) { Alert a = new Alert(""); a.setString(err); a.setTimeout(Alert.FOREVER); display.setCurrent(a); }
private void handleMainMenu() { int index = dmenu.getSelectedIndex(); switch(index) { case 0 : if (user != null) { sendeMail(); break; } case 1 : status = 1; loginUser(); break; } }
/* This method ask user to compose the message*/ private void sendeMail() { List menu = new List("Send Message"); to = new TextBox("Enter Reciepent :", "", 50, TextField.ANY); msg = new TextBox("Enter Message :", "", 250, TextField.ANY); menu.append(to); menu.append(msg); menu.addCommand(okCommand); menu.addCommand(exitCommand); menu.setCommandListener(this); display.setCurrent(menu); }
public class SendThread implements runnable { String user; String pwd; String to; String msg;
public SendThread(String user,String to,String msg) { int j user.indexOf('/'); if (j > 0) { user = user.substring(0,j); pwd = user.substring(j+1); to = to; msg = msg; } }
/*This method gets the response after sending the mail*/ public String getResponseMessage() { return responseMessage; }
/* This method send POST request to servlet with necessary params*/ public void run() { HttpConnection hc = null; OutputStream out = null; try { hc = (HttpConnection)Connector.open(url); hc.setRequestMethod(HttpConnection.POST); hc.setRequestProperty("Content-Type", "text/plain"); out = hc.openOutputStream(); PrintStream pout = new PrintStream(out); pout.println(user); pout.println(pwd); pout.println(mEmail); pout.println(mMessage);
InputStream in = hc.openInputStream(); int length = (int)hc.getLength(); if (length == -1) length = 255; byte[] raw = new byte[length]; in.read(raw); String s = new String(raw); String codeString = s.substring(0, 4).trim(); responseMessage = s.substring(4).trim(); } finally { if (hc != null) hc.close(); if (out != null) out.close(); } } } }
/* This method form Mail and send to server*/ private void sendMail(String server, String user, String pwd, String address, String content) throws Exception {
Properties p = new Properties(); p.put("mail.smtp.host", server); Session s = Session.getDefaultInstance(p, null); InternetAddress from = new InternetAddress(user); InternetAddress to = new InternetAddress(address); MimeMessage m = new MimeMessage(s); m.setFrom(from); m.addRecipient(Message.RecipientType.TO, to); String now = mDateFormat.format(new java.util.Date()); m.setSubject("Mail from [" + from + "]"); m.setText(content.toString()); Transport.send(m); }