| Код | import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*;
public class EmailMIDlet extends MIDlet implements CommandListener { Display display = null;
// email form fields TextField toField = null; TextField subjectField = null; TextField msgField = null; Form form;
static final Command sendCommand = new Command("send", Command.OK, 2); static final Command clearCommand = new Command("clear", Command.STOP, 3); String to; String subject; String msg;
public EmailMIDlet() { display = Display.getDisplay(this); form = new Form("Compose Message"); toField = new TextField("To:", "", 25, TextField.EMAILADDR); subjectField = new TextField("Subject:", "", 15, TextField.ANY); msgField = new TextField("MsgBody:", "", 90, TextField.ANY); }
public void startApp() throws MIDletStateChangeException { form.append(toField); form.append(subjectField); form.append(msgField); form.addCommand(clearCommand); form.addCommand(sendCommand); form.setCommandListener(this); display.setCurrent(form); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { notifyDestroyed(); }
public void commandAction(Command c, Displayable d) { String label = c.getLabel(); if(label.equals("clear")) { destroyApp(true); } else if (label.equals("send")) { to = toField.getString(); subject = subjectField.getString(); msg = msgField.getString(); EmailClient client = new EmailClient (this,"[email protected]", to, subject, msg); client.start(); } } }
|
| Код |
import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import java.io.*; import java.util.*;
public class EmailClient implements Runnable { private EmailMIDlet parent; private Display display; private Form f; private StringItem si; private SocketConnection sc; private InputStream is; private OutputStream os; private String smtpServerAddress;
private String from, to, subject, msg;
public EmailClient (EmailMIDlet m, String from, String to, String subject, String msg) { parent = m; this.from = from; this.to = to; this.subject = subject; this.msg = msg;
display = Display.getDisplay(parent); f = new Form("Email Client"); si = new StringItem("Response:" , " "); f.append(si); display.setCurrent(f); }
public void start() { Thread t = new Thread(this); t.start(); }
public void run() { try { sc = (SocketConnection) Connector.open("socket://"+smtpServerAddress+":25"); is = sc.openInputStream(); os = sc.openOutputStream();
os.write(("HELO there" + "\r\n").getBytes()); os.write(("MAIL FROM: "+ from +"\r\n").getBytes()); os.write(("RCPT TO: "+ to + "\r\n").getBytes()); os.write("DATA\r\n".getBytes()); // stamp the msg with date os.write(("Date: " + new Date() + "\r\n").getBytes()); os.write(("From: "+from+"\r\n").getBytes()); os.write(("To: "+to+"\r\n").getBytes()); os.write(("Subject: "+subject+"\r\n").getBytes()); os.write((msg+"\r\n").getBytes()); // message body os.write(".\r\n".getBytes()); os.write("QUIT\r\n".getBytes());
// debug StringBuffer sb = new StringBuffer(); int c = 0; while (((c = is.read()) != -1) ) { sb.append((char) c); } si.setText("SMTP server response - " + sb.toString());
} catch(IOException e) {
Alert a = new Alert ("TimeClient", "Cannot connect to SMTP server. Ping the server to make sure it is running...", null, AlertType.ERROR); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } finally { try { if(is != null) { is.close(); } if(os != null) { os.close(); } if(sc != null) { sc.close(); } } catch(IOException e) { e.printStackTrace(); } } }
public void commandAction(Command c, Displayable s) { if (c == Alert.DISMISS_COMMAND) { parent.notifyDestroyed(); parent.destroyApp(true); } } }
|
--------------------
01101010 01100001 01110110 01100001 01110011 01110100 01101001 01100011 scjp, mcp
|