扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
class MailThread extends Thread
{
private MailClient midlet;
public MailThread(MailClient midlet)
{
this.midlet = midlet;
}
public void run()
{
synchronized(midlet)
{
try
{
midlet.wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("connecting to server.....");
HttpConnection httpConn = null;
DataOutputStream dos = null;
try
{
httpConn = (HttpConnection)Connector.open("http://localhost:8088/mail/maildo");
httpConn.setRequestMethod("POST");
dos = new DataOutputStream(httpConn.openOutputStream());
dos.writeUTF(midlet.getMessage().getTo());
dos.writeUTF(midlet.getMessage().getSubject());
dos.writeUTF(midlet.getMessage().getContent());
dos.close();
httpConn.close();
System.out.println("end of sending mail");
}
catch(IOException e)
{}
}}
在服务器端,我们要完成自己的servlet。他的任务比较简单就是接受客户端的数据然后通过JavaMail发送到指定的收件人那里。如果您对JavaMail还不熟悉请参考如下文章。这里直接给出servlet代码。
使用JavaMail实现收发电子邮件功能
使用Servlet发送电子邮件
package com.j2medev.servletmail;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;
import java.net.*;
public class MailServlet extends HttpServlet
{
private static String host;
private static String from;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
host = config.getInitParameter("host");
from = config.getInitParameter("from");
System.out.println(host + from);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
DataInputStream dis = new DataInputStream(request.getInputStream());
String send = dis.readUTF();
String subject = dis.readUTF();
String content = dis.readUTF();
try
{
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
send));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(content);
// Send message
Transport.send(message);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ServletMail</servlet-name>
<servlet-class>com.j2medev.servletmail.MailServlet</servlet-class>
<init-param>
<param-name>host</param-name>
<param-value>smtp.263.net</param-value>
</init-param>
<init-param>
<param-name>from</param-name>
<param-value>eric.zhan@263.net</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletMail</servlet-name>
<url-pattern>/maildo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
总结:本文更多要介绍的其实还是这个代理的思想,利用所学的知识灵活应用。不要局限于J2ME提供给你的API。文章中实现的客户端服务器都比较简单,也不够友好,如果感兴趣可以稍微修饰一下,对提高能力还是有帮助的。在MIDP中只是提供了RMS作为持久存储用,因此实现接受存储邮件不是很方便。如果手机支持FileConnection的话可以编写一个接受邮件的客户端。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者