Simple email send function with J2ME

xiaoxiao2021-04-09  361

In GCF, there is no supply to us to send an email, and the optional package of J2ME does not provide relevant functions. So can we use J2ME to send an email function? The answer is yes. This article will mainly tell how to implement the function of sending emails in J2ME.

A very important thinking here is a proxy. We know that GCF is available to us, such as through HTTP. The API of the Socket Network is even provided in MIDP2.0. Then we can connect the server-side programs such as servlets, then servlet can send emails through the interface provided by JavaMail. Then we need to do it through the HTTP protocol or other protocol to send the title, content, recipient, etc. of the email to the servlet. This is very flexible and very flexible.

First we construct a Message class to represent the message sent. It includes three fields of the subject, recipients, and content. 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 () {returnid;}

Public void setContent (string content) {this.content = content;} public string getsubject () {return.com

Public void setsubject (string subject) {this.subject = Subject;}

Public string getto () {returnide} {this.to = to;} public string TOSTRING () {Return to Subject Content;}}

In the design of the user interface, we need two interfaces. One allows users to enter recipients and topics, and the other for collecting content entered. Since TextBox is to be exclusively, we can't put them together.

/ * * 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; 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) {}

}}}

Finally, we have completed the MIDlet, including the networking program code, because this site has provided a lot of introduction about J2ME networking, so there is no longer explained here. Package com.j2medev.mail;

Import java.io.DataOutputStream; import java.io.ioException;

Import javax.microedition.midlet.mIdlet; import javax.microedition.midlet.midletStateChangeException; import javax.microedition.lcdui. *; import javax.microedition.io. *;

Public class mailclient extends midlet {private mainform mainform;

Private contentform contentform;

Private display display;

Private Message Message; Public Message GetMessage () {return message;

Public void setmessage (message message) {this.Message = mess;}

public void displayAlert (String text, AlertType type, Displayable disp) {Alert alert = new Alert ( "Application Error"); alert.setString (text); alert.setType (type); alert.setTimeout (2000); display.setCurrent (Alert, DISP);

}

Public Contentform getContentform () {Return ContentForm;

Public Display getDisplay () {Return Display;

Public Mainform getMainform () {Return Mainform;}

public void initMIDlet () {MailThread t = new MailThread (this); t.start (); message = new Message (); display = Display.getDisplay (this); mainForm = new MainForm (this, "Simple Mail Client") Contentform = New Contentform ("Content", NULL, 150, TEXTFIELD.ANY, THIS); Display.SetCurrent (mainform);}

Protected void Startapp () throws midletStateChangeException {initmidlet ();

}

protected void pauseapp () {

}

Protected Void DestroyApp (Boolean Arg0) throws midletStateChangeException {

}

}

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 send mail");} catch (ioException e) {}}} At the server side, we have to complete your servlet . His task is relatively simple is to accept the client's data and then send it to the specified recipient via JavaMail. If you are not familiar with JavaMail, please refer to the following article. The servlet code is given directly here. Use Javamail to send and receive email features to send emails with servlet

Package com.j2medev.servletmail;

Import java.io.DataInputStream; import java.io iexception; 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, oException {

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 servletmail com.j2medev.servletmail.mailServlet Host smtp.263.net from Eric.zhan@263.net servletmail / maildo index.jsp

404 / error.jsp

Summary: This article is more important to introduce the idea of ​​this agent, and use the knowledge you have learned. Don't be limited to the API provided to your J2ME. The client server implemented in the article is relatively simple, and it is not friendly. If you are interested, you can be slightly modified, and it is also helpful to improve your ability. In the MIDP, only RMS is provided as a persistent storage, so it is not very convenient to implement storage emails. If the phone supports fileconnection, you can write a client that accepts the message.

转载请注明原文地址:https://www.9cbs.com/read-132993.html

New Post(0)