📄 j2me实现简单电子邮件发送功能.mht
字号:
<P><BR>public class ContentForm extends TextBox implements=20
CommandListener<BR>{<BR> private MailClient =
midlet;</P>
<P> private boolean first =3D true;</P>
<P> public static final Command sendCommand =3D new=20
Command("SEND",=20
Command.ITEM,<BR> &n=
bsp; =20
1);</P>
<P> public ContentForm(String arg0, String arg1, int =
arg2, int=20
arg3,<BR> &nbs=
p;=20
MailClient midlet)<BR> =20
{<BR> super(arg0, arg1, arg2,=20
arg3);<BR> this.midlet =3D=20
midlet;<BR> if=20
(first)<BR> =20
{<BR> =
first =3D=20
false;<BR> &nb=
sp;=20
init();<BR> }</P>
<P> }</P>
<P> public void init()<BR> =20
{<BR> =20
this.addCommand(sendCommand);<BR> &nbs=
p;=20
this.setCommandListener(this);<BR> }</P>
<P> public void commandAction(Command cmd, Displayable =
disp)<BR> =
{<BR> if=20
(cmd =3D=3D sendCommand)<BR> =20
{<BR> =
String=20
content =3D=20
this.getString();<BR> &nbs=
p; =20
midlet.getMessage().setContent(content);<BR>  =
; =20
System.out.println(midlet.getMessage());<BR>  =
; =20
try<BR> =
=20
{<BR> &n=
bsp; =20
synchronized=20
(midlet)<BR> &=
nbsp; =20
{<BR> &n=
bsp; =20
midlet.notify();<BR>  =
; =20
}</P>
<P> } =
catch=20
(Exception=20
e)<BR> =
{<BR> =
}</P>
<P> }<BR> =
}<BR>}</P>
<P> =20
=D7=EE=BA=F3=CE=D2=C3=C7=CD=EA=B3=C9MIDlet=A3=AC=D4=DA=C6=E4=D6=D0=B0=FC=C0=
=A8=C1=AA=CD=F8=B5=C4=B3=CC=D0=F2=B4=FA=C2=EB=A3=AC=D3=C9=D3=DA=B1=BE=D5=BE=
=D2=D1=BE=AD=CC=E1=B9=A9=C1=CB=BA=DC=B6=E0=B9=D8=D3=DAJ2ME=C1=AA=CD=F8=B5=
=C4=BD=E9=C9=DC=A3=AC=D2=F2=B4=CB=D5=E2=C0=EF=B2=BB=D4=D9=BD=F8=D0=D0=B8=FC=
=B6=E0=B5=C4=BD=E2=CA=CD=A1=A3<BR><BR>package=20
com.j2medev.mail;</P>
<P>import java.io.DataOutputStream;<BR>import java.io.IOException;</P>
<P>import javax.microedition.midlet.MIDlet;<BR>import=20
javax.microedition.midlet.MIDletStateChangeException;<BR>import=20
javax.microedition.lcdui.*;<BR>import javax.microedition.io.*;</P>
<P><BR>public class MailClient extends MIDlet<BR>{<BR> =
private=20
MainForm mainForm;</P>
<P> private ContentForm contentForm;</P>
<P> private Display display;</P>
<P> private Message message;</P>
<P> public Message getMessage()<BR> =20
{<BR> return=20
message;<BR> }</P>
<P> public void setMessage(Message=20
message)<BR> =
{<BR> =20
this.message =3D message;<BR> }</P>
<P> public void displayAlert(String text, AlertType =
type,=20
Displayable disp)<BR> =20
{<BR> Alert alert =3D new=20
Alert("Application =
Error");<BR> =20
alert.setString(text);<BR> =20
alert.setType(type);<BR> =20
alert.setTimeout(2000);<BR> =20
display.setCurrent(alert, disp);</P>
<P> }</P>
<P><BR> public ContentForm=20
getContentForm()<BR> =20
{<BR> return=20
contentForm;<BR> }</P>
<P> public Display getDisplay()<BR> =20
{<BR> return=20
display;<BR> }</P>
<P><BR> public MainForm =
getMainForm()<BR> =20
{<BR> return=20
mainForm;<BR> }</P>
<P> public void initMIDlet()<BR> =20
{<BR> MailThread t =3D new=20
MailThread(this);<BR> =20
t.start();<BR> message =3D new =
Message();<BR> display =3D=20
Display.getDisplay(this);<BR> =
mainForm=20
=3D new MainForm(this, "Simple Mail=20
Client");<BR> contentForm =3D =
new=20
ContentForm("Content", null, 150, TextField.ANY,=20
this);<BR> =20
display.setCurrent(mainForm);<BR> }</P>
<P><BR> protected void startApp() throws=20
MIDletStateChangeException<BR> =20
{<BR> =20
<BR> initMIDlet();</P>
<P> }</P>
<P> protected void pauseApp()<BR> =20
{<BR> </P>
<P> }</P>
<P> protected void destroyApp(boolean arg0) throws=20
MIDletStateChangeException<BR> =20
{<BR> </P>
<P> }</P>
<P>}</P>
<P>class MailThread extends Thread<BR>{<BR> private =
MailClient=20
midlet;</P>
<P> public MailThread(MailClient =
midlet)<BR> =20
{<BR> this.midlet =3D=20
midlet;<BR> }</P>
<P>public void run()<BR> =20
{<BR> =20
synchronized(midlet)<BR> =20
{<BR> =20
try<BR> =
=20
{<BR> &n=
bsp; =20
midlet.wait();<BR> &=
nbsp; =20
}<BR> =20
catch(Exception=20
e)<BR> =
{<BR> &n=
bsp; =20
e.printStackTrace();<BR> &=
nbsp; =20
}<BR> =20
<BR> =20
}<BR> =
System.out.println("connecting=20
to server.....");<BR> =
HttpConnection=20
httpConn =3D null;<BR> =
DataOutputStream=20
dos =3D null;<BR> =20
<BR> =20
try<BR> =20
{<BR> =
httpConn=20
=3D (HttpConnection)Connector.open("<A=20
href=3D"http://localhost:8088/mail/maildo">http://localhost:8088/mail/mai=
ldo</A>");<BR>  =
; =20
httpConn.setRequestMethod("POST");<BR>  =
; =20
dos =3D new=20
DataOutputStream(httpConn.openOutputStream());<BR>  =
; =20
dos.writeUTF(midlet.getMessage().getTo());<BR> &nb=
sp; =20
dos.writeUTF(midlet.getMessage().getSubject());<BR> &nbs=
p; =20
dos.writeUTF(midlet.getMessage().getContent());<BR> &nbs=
p; =20
dos.close();<BR> &nb=
sp; =20
httpConn.close();<BR> &nbs=
p; =20
System.out.println("end of sending=20
mail");<BR> =20
}<BR> catch(IOException=20
e)<BR> =
{}<BR> }}</P>
<P> =20
=D4=DA=B7=FE=CE=F1=C6=F7=B6=CB=A3=AC=CE=D2=C3=C7=D2=AA=CD=EA=B3=C9=D7=D4=BC=
=BA=B5=C4servlet=A1=A3=CB=FB=B5=C4=C8=CE=CE=F1=B1=C8=BD=CF=BC=F2=B5=A5=BE=
=CD=CA=C7=BD=D3=CA=DC=BF=CD=BB=A7=B6=CB=B5=C4=CA=FD=BE=DD=C8=BB=BA=F3=CD=A8=
=B9=FDJavaMail=B7=A2=CB=CD=B5=BD=D6=B8=B6=A8=B5=C4=CA=D5=BC=FE=C8=CB=C4=C7=
=C0=EF=A1=A3=C8=E7=B9=FB=C4=FA=B6=D4JavaMail=BB=B9=B2=BB=CA=EC=CF=A4=C7=EB=
=B2=CE=BF=BC=C8=E7=CF=C2=CE=C4=D5=C2=A1=A3=D5=E2=C0=EF=D6=B1=BD=D3=B8=F8=B3=
=F6servlet=B4=FA=C2=EB=A1=A3<BR><A=20
href=3D"http://www.j2medev.com/Article/Class1/Class14/200412/Article_Show=
.asp?ArticleID=3D169">=CA=B9=D3=C3JavaMail=CA=B5=CF=D6=CA=D5=B7=A2=B5=E7=D7=
=D3=D3=CA=BC=FE=B9=A6=C4=DC=20
</A><BR><A=20
href=3D"http://www.j2medev.com/Article/Class1/Class14/200412/Article_Show=
.asp?ArticleID=3D171">=CA=B9=D3=C3Servlet=B7=A2=CB=CD=B5=E7=D7=D3=D3=CA=BC=
=FE=20
</A></P>
<P><BR>package com.j2medev.servletmail;</P>
<P>import java.io.DataInputStream;<BR>import =
java.io.IOException;<BR>import=20
java.util.Properties;</P>
<P>import javax.servlet.ServletConfig;<BR>import=20
javax.servlet.ServletException;<BR>import=20
javax.servlet.http.HttpServlet;<BR>import=20
javax.servlet.http.HttpServletRequest;<BR>import=20
javax.servlet.http.HttpServletResponse;<BR>import =
javax.mail.*;<BR>import=20
javax.mail.internet.InternetAddress;<BR>import=20
javax.mail.internet.MimeMessage;<BR>import java.util.*;<BR>import=20
java.net.*;</P>
<P>public class MailServlet extends =
HttpServlet<BR>{<BR> =20
private static String host;</P>
<P> private static String from;</P>
<P> public void init(ServletConfig config) throws=20
ServletException<BR> =20
{<BR> =20
super.init(config);<BR> host =
=3D=20
config.getInitParameter("host");<BR> &=
nbsp;=20
from =3D=20
config.getInitParameter("from");<BR> &=
nbsp;=20
System.out.println(host + from);<BR> }</P>
<P> protected void doGet(HttpServletRequest=20
request,<BR> &=
nbsp;=20
HttpServletResponse response) throws ServletException,=20
IOException<BR> {</P>
<P> doPost(request,=20
response);<BR> }</P>
<P> protected void doPost(HttpServletRequest=20
request,<BR> &=
nbsp;=20
HttpServletResponse response) throws ServletException,=20
IOException<BR> =
{<BR> =20
<BR> DataInputStream dis =3D =
new=20
DataInputStream(request.getInputStream());<BR> &nb=
sp; =20
String send =3D =
dis.readUTF();<BR> =20
String subject =3D =
dis.readUTF();<BR> =20
String content =3D =
dis.readUTF();<BR> =20
try<BR> =20
{<BR> =20
Properties props =3D=20
System.getProperties();<BR> &nbs=
p; =20
// Setup mail=20
server<BR> &nb=
sp;=20
props.put("mail.smtp.host",=20
host);<BR> &nb=
sp; //=20
Get=20
session<BR> &n=
bsp;=20
Session session =3D Session.getDefaultInstance(props,=20
null);<BR> &nb=
sp; //=20
Define=20
message<BR> &n=
bsp;=20
MimeMessage message =3D new=20
MimeMessage(session);<BR> =
=20
// Set the from=20
address<BR> &n=
bsp;=20
message.setFrom(new=20
InternetAddress(from));<BR> &nbs=
p; =20
// Set the to=20
address<BR> &n=
bsp;=20
message.addRecipient(Message.RecipientType.TO, new=20
InternetAddress(<BR>  =
; =20
send));<BR> &n=
bsp; //=20
Set the=20
subject<BR> &n=
bsp;=20
message.setSubject(subject);<BR>  =
; =20
// Set the=20
content<BR> &n=
bsp;=20
message.setText(content);<BR> &n=
bsp; =20
// Send=20
message<BR> &n=
bsp;=20
Transport.send(message);<BR> } =
catch=20
(Exception e)<BR> =20
{<BR> =20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -