這裡一個非常重要的思想就是代理。我們知道GCF提供給我們進行聯網的能力了,比如通過Http聯網。在MIDP2.0中甚至提供了socket聯網的API。那麼我們可以通過他們連接服務器端的程序比如servlet,然後servlet可以通過JavaMail提供的接口發送郵件。那麼我們需要做的只是通過Http協議或者其他協議把郵件的標題、內容、收件人等發送給servlet。就是這個簡單的思想卻是非常靈活非常有用。
首先我們構造一個Message類來代表發送的消息。它包括主題、收件人和內容三個字段。
package com.J2MEdev.mail;
public class Message
{ private String to;
private String subject;
private String content;
public Message()
{
}
public Message(String to, String subject, String content)
{
this.to = to;
this.subject = subject;
this.content = content;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getTo()
{
return to;
}
public void setTo(String to)
{
this.to = to;
}
public String toString()
{
return to+subject+content;
}
}
在用戶界面的設計上,我們需要兩個界面。一個讓用戶輸入收件人和主題,另一個用於收集用戶輸入的內容。由於TextBox要獨占一個屏幕的,因此我們不能把他們放在一起。
/*
* Created on 2004-12-8
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.J2MEdev.mail;import Javax.microedition.lcdui.Form;
import Javax.microedition.lcdui.Item;
import Javax.microedition.lcdui.*;
import Javax.microedition.midlet.MIDlet;/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class MainForm extends Form implements CommandListener
{private MailClIEnt midlet;
private TextField toFIEld;
private TextField subFIEld;
private boolean first = true;
public static final Command nextCommand = new Command("NEXT", Command.OK, 1);public MainForm(MailClIEnt midlet, String arg0)
{
super(arg0);
this.midlet = midlet;
if(first)
{
first = false;
init();
}
}public void init()
{
toField = new TextField("To:", null, 25, TextFIEld.ANY);
subField = new TextField("Subject:", null, 30, TextFIEld.ANY);
this.append(toFIEld);
this.append(subFIEld);
this.addCommand(nextCommand);
this.setCommandListener(this);
}
public void commandAction(Command cmd,Displayable disp)
{
if(cmd == nextCommand)
{
String to = toFIEld.getString();
String subject = subFIEld.getString();
if(to == "" && subject == "")
{
midlet.displayAlert("Null to or sub",AlertType.WARNING,this);
}
else
{
midlet.getMessage().setTo(to);
midlet.getMessage().setSubject(subject);
midlet.getDisplay().setCurrent(midlet.getContentForm());
}
}
}
}package com.J2MEdev.mail;
import Javax.microedition.lcdui.Command;
import Javax.microedition.lcdui.CommandListener;
import Javax.microedition.lcdui.Displayable;
import Javax.microedition.lcdui.TextBox;
import Javax.microedition.midlet.MIDlet;public class ContentForm extends TextBox implements CommandListener
{
private MailClIEnt midlet;
private boolean first = true;
public static final Command sendCommand = new Command("SEND", Command.ITEM,
1);public ContentForm(String arg0, String arg1, int arg2, int arg3,
MailClIEnt midlet)
{
super(arg0, arg1, arg2, arg3);
this.midlet = midlet;
if (first)
{
first = false;
init();
}}
public void init()
{
this.addCommand(sendCommand);
this.setCommandListener(this);
}public void commandAction(Command cmd, Displayable disp)
{
if (cmd == sendCommand)
{
String content = this.getString();
midlet.getMessage().setContent(content);
System.out.println(midlet.getMessage());
try
{
synchronized (midlet)
{
midlet.notify();
}} catch (Exception e)
{
}
}
}
}