⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmail.java

📁 J2ME 无线通信技术应用开发源代码
💻 JAVA
字号:
//SendMail.java,项目SendMail
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;

public class SendMail extends MIDlet implements CommandListener {
    Display display = null;

    TextField toField = null;
    TextField subjectField = null;
    TextField msgField = null;
    Form form;

    String url = "http://localhost:8080/j2me/servlet/EmailServlet";
    static final Command sendCmd = new Command("发送", Command.OK, 2);
    static final Command clearCmd = new Command("清除", Command.STOP, 3);

    String to;
    String subject;
    String msg;


    public SendMail() {
       display = Display.getDisplay(this);
       toField = new TextField("收件人地址:", "", 25, TextField.EMAILADDR);
       subjectField = new TextField("邮件主题:", "", 15, TextField.ANY);
       msgField = new TextField("邮件内容:", "", 90, TextField.ANY);
       form = new Form("发邮件");

    }

    public void startApp() throws MIDletStateChangeException {
      form.append(toField);
      form.append(subjectField);
      form.append(msgField);
      form.addCommand(clearCmd);
      form.addCommand(sendCmd);
      form.setCommandListener(this);
      display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
      notifyDestroyed();
    }


    void invokeServlet(String url) throws IOException {
        HttpConnection c = null;
        InputStream is = null;
        OutputStream os = null;
        StringBuffer b = new StringBuffer();
        TextBox t = null;
        try {
          c = (HttpConnection)Connector.open(url);
          c.setRequestMethod(HttpConnection.POST);
          c.setRequestProperty("IF-Modified-Since", "20 Oct 2001 16:19:14 GMT");
          c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
          c.setRequestProperty("Content-Language", "en-CA");
          c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          
          os = c.openOutputStream();
          os.write(("to="+to).getBytes());
          os.write(("&subject="+subject).getBytes());
          os.write(("&msg="+msg).getBytes());
          os.flush();

          is = c.openDataInputStream();
          int ch;
          while ((ch = is.read()) != -1) {
            b.append((char) ch);
            System.out.print((char)ch);
          }
          t = new TextBox("确认信息", b.toString(), 1024, 0);
          t.setCommandListener(this);
        } finally {
           if(is!= null) {
              is.close();
           }
           if(os != null) {
              os.close();
           }
           if(c != null) {
              c.close();
           }
        }
        display.setCurrent(t);
    } 

   public void commandAction(Command c, Displayable d) {
      if(c == clearCmd) {
        destroyApp(true);
      } else if (c == sendCmd) {
         to = toField.getString();
         subject = subjectField.getString();
         msg = msgField.getString();
         try {
           invokeServlet(url);
         }catch(IOException e) {}
      } 
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -