home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Mail For Me / Mail4ME.jar / de / trantor / mail / demo / j2me / MailMIDlet.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-10-21  |  10.0 KB  |  250 lines

  1. package de.trantor.mail.demo.j2me;
  2.  
  3. import de.trantor.mail.ImapClient;
  4. import de.trantor.mail.InboxClient;
  5. import de.trantor.mail.Message;
  6. import de.trantor.mail.MimeDecoder;
  7. import de.trantor.mail.Pop3Client;
  8. import de.trantor.mail.Pop3Exception;
  9. import de.trantor.mail.SmtpClient;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.util.Vector;
  16. import javax.microedition.lcdui.Alert;
  17. import javax.microedition.lcdui.Command;
  18. import javax.microedition.lcdui.CommandListener;
  19. import javax.microedition.lcdui.Display;
  20. import javax.microedition.lcdui.Displayable;
  21. import javax.microedition.lcdui.Image;
  22. import javax.microedition.midlet.MIDlet;
  23. import javax.microedition.midlet.MIDletStateChangeException;
  24. import javax.microedition.rms.RecordStore;
  25. import javax.microedition.rms.RecordStoreException;
  26.  
  27. public class MailMIDlet extends MIDlet implements CommandListener {
  28.    private Display display = Display.getDisplay(this);
  29.    private SetupScreen setupScreen = new SetupScreen(this);
  30.    private InboxScreen inboxScreen = new InboxScreen(this);
  31.    private ReadScreen readScreen = new ReadScreen(this);
  32.    private WriteScreen writeScreen = new WriteScreen(this);
  33.    private String address = "yourname@yourisp.com";
  34.    private String hostname = "yourhost.yourisp.com";
  35.    private String pop3Host = "pop.yourisp.com";
  36.    private String pop3User = "your-username";
  37.    private String pop3Pass = "your-password";
  38.    private String smtpHost = "smtp.yourisp.com";
  39.    private boolean debug = false;
  40.    private boolean imap = false;
  41.    private InboxClient pop3Client;
  42.    private Vector msgNumbers = new Vector();
  43.  
  44.    public MailMIDlet() {
  45.       try {
  46.          this.loadSetupData();
  47.       } catch (Exception var2) {
  48.          ((Throwable)var2).printStackTrace();
  49.       }
  50.  
  51.       this.setupScreen.setAddress(this.address);
  52.       this.setupScreen.setHostname(this.hostname);
  53.       this.setupScreen.setPop3Host(this.pop3Host);
  54.       this.setupScreen.setPop3User(this.pop3User);
  55.       this.setupScreen.setPop3Pass(this.pop3Pass);
  56.       this.setupScreen.setSmtpHost(this.smtpHost);
  57.       this.setupScreen.setDebug(this.debug);
  58.       this.setupScreen.setImap(this.imap);
  59.    }
  60.  
  61.    public void commandAction(Command cmd, Displayable dsp) {
  62.       System.out.println("Command: " + cmd.getLabel());
  63.  
  64.       try {
  65.          if (cmd == SetupScreen.OK) {
  66.             this.address = this.setupScreen.getAddress();
  67.             this.hostname = this.setupScreen.getHostname();
  68.             this.pop3Host = this.setupScreen.getPop3Host();
  69.             this.pop3User = this.setupScreen.getPop3User();
  70.             this.pop3Pass = this.setupScreen.getPop3Pass();
  71.             this.smtpHost = this.setupScreen.getSmtpHost();
  72.             this.debug = this.setupScreen.getDebug();
  73.             this.imap = this.setupScreen.getImap();
  74.  
  75.             try {
  76.                this.saveSetupData();
  77.             } catch (Exception var12) {
  78.                ((Throwable)var12).printStackTrace();
  79.             }
  80.  
  81.             if (this.imap) {
  82.                this.pop3Client = new ImapClient();
  83.             } else {
  84.                this.pop3Client = new Pop3Client();
  85.             }
  86.  
  87.             this.pop3Client.setDebug(this.debug);
  88.             this.pop3Client.open(this.pop3Host, this.pop3User, this.pop3Pass);
  89.             this.getMessageList();
  90.             this.display.setCurrent(this.inboxScreen);
  91.          } else if (cmd == InboxScreen.READ) {
  92.             int num = this.inboxScreen.getMessageIndex();
  93.             if (num == -1) {
  94.                return;
  95.             }
  96.  
  97.             Message message = this.pop3Client.getMessage((Integer)this.msgNumbers.elementAt(num));
  98.             this.readScreen.setSender(Message.getMachineAddress(message.getHeaderValue("From", "No sender")));
  99.             this.readScreen.setDate(message.getHeaderValue("Date", "No date"));
  100.             this.readScreen.setSubject(message.getHeaderValue("Subject", "No subject"));
  101.             this.readScreen.clearBody();
  102.             MimeDecoder mime = new MimeDecoder(message);
  103.             this.addPartToScreen(mime);
  104.             this.display.setCurrent(this.readScreen);
  105.          } else if (cmd == InboxScreen.WRITE) {
  106.             this.writeScreen.clear();
  107.             this.display.setCurrent(this.writeScreen);
  108.          } else if (cmd == InboxScreen.DELETE) {
  109.             int num = this.inboxScreen.getMessageIndex();
  110.             if (num == -1) {
  111.                return;
  112.             }
  113.  
  114.             this.pop3Client.removeMessage((Integer)this.msgNumbers.elementAt(num));
  115.             this.inboxScreen.delete(this.inboxScreen.getMessageIndex());
  116.             this.msgNumbers.removeElementAt(num);
  117.             this.display.setCurrent(this.inboxScreen);
  118.          } else if (cmd == InboxScreen.EXIT) {
  119.             this.destroyApp(false);
  120.             ((MIDlet)this).notifyDestroyed();
  121.          } else if (cmd == ReadScreen.OK) {
  122.             this.display.setCurrent(this.inboxScreen);
  123.          } else if (cmd == WriteScreen.OK) {
  124.             Message message = new Message(this.address, this.writeScreen.getRecipient(), this.writeScreen.getSubject());
  125.             message.addBodyLine(this.writeScreen.getBody());
  126.             SmtpClient smtpClient = new SmtpClient(this.hostname);
  127.  
  128.             try {
  129.                smtpClient.setDebug(this.debug);
  130.                smtpClient.open(this.smtpHost);
  131.                smtpClient.sendMessage(message);
  132.             } finally {
  133.                smtpClient.close();
  134.             }
  135.  
  136.             this.display.setCurrent(this.inboxScreen);
  137.          } else if (cmd == WriteScreen.CANCEL) {
  138.             this.display.setCurrent(this.inboxScreen);
  139.          }
  140.       } catch (Exception var13) {
  141.          Alert alert = new Alert("Error");
  142.          alert.setString(var13.getClass().getName() + ": " + ((Throwable)var13).getMessage());
  143.          ((Throwable)var13).printStackTrace();
  144.          this.display.setCurrent(alert, this.setupScreen);
  145.       }
  146.  
  147.    }
  148.  
  149.    protected void startApp() throws MIDletStateChangeException {
  150.       this.display.setCurrent(this.setupScreen);
  151.    }
  152.  
  153.    protected void destroyApp(boolean unconditional) {
  154.       try {
  155.          this.pop3Client.close();
  156.       } catch (Exception var3) {
  157.       }
  158.  
  159.    }
  160.  
  161.    protected void pauseApp() {
  162.    }
  163.  
  164.    private void saveSetupData() throws RecordStoreException, IOException {
  165.       try {
  166.          RecordStore.deleteRecordStore("mail4me");
  167.       } catch (RecordStoreException var9) {
  168.       }
  169.  
  170.       RecordStore store = RecordStore.openRecordStore("mail4me", true);
  171.  
  172.       try {
  173.          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  174.          DataOutputStream output = new DataOutputStream(buffer);
  175.          output.writeUTF(this.address);
  176.          output.writeUTF(this.hostname);
  177.          output.writeUTF(this.pop3Host);
  178.          output.writeUTF(this.pop3User);
  179.          output.writeUTF(this.pop3Pass);
  180.          output.writeUTF(this.smtpHost);
  181.          output.writeBoolean(this.debug);
  182.          output.writeBoolean(this.imap);
  183.          store.addRecord(buffer.toByteArray(), 0, buffer.size());
  184.       } finally {
  185.          store.closeRecordStore();
  186.       }
  187.  
  188.    }
  189.  
  190.    private void loadSetupData() throws RecordStoreException, IOException {
  191.       try {
  192.          RecordStore store = RecordStore.openRecordStore("mail4me", false);
  193.  
  194.          try {
  195.             ByteArrayInputStream buffer = new ByteArrayInputStream(store.getRecord(1));
  196.             DataInputStream input = new DataInputStream(buffer);
  197.             this.address = input.readUTF();
  198.             this.hostname = input.readUTF();
  199.             this.pop3Host = input.readUTF();
  200.             this.pop3User = input.readUTF();
  201.             this.pop3Pass = input.readUTF();
  202.             this.smtpHost = input.readUTF();
  203.             this.debug = input.readBoolean();
  204.             this.imap = input.readBoolean();
  205.          } finally {
  206.             store.closeRecordStore();
  207.          }
  208.       } catch (Exception var9) {
  209.       }
  210.  
  211.    }
  212.  
  213.    private void getMessageList() throws Pop3Exception, IOException {
  214.       Vector list = new Vector();
  215.       int count = this.pop3Client.getMessageCount();
  216.  
  217.       for(int i = 0; i < count; ++i) {
  218.          Message message = this.pop3Client.getHeaders(i);
  219.          list.addElement(message.getHeaderValue("Subject", "No subject") + " (" + Message.getMachineAddress(message.getHeaderValue("From", "No sender") + ")"));
  220.          this.msgNumbers.insertElementAt(new Integer(i), 0);
  221.       }
  222.  
  223.       this.inboxScreen.setMessages(list);
  224.    }
  225.  
  226.    private void addPartToScreen(MimeDecoder mime) {
  227.       if (mime.getPartCount() == 0) {
  228.          if (mime.getType().equals("image/png")) {
  229.             byte[] bytes = mime.getBodyBytes();
  230.             this.readScreen.addImage(Image.createImage(bytes, 0, bytes.length));
  231.          } else if (mime.getType() != null && !mime.getType().equals("text/plain")) {
  232.             this.readScreen.addBody("\n[Unable to display \"" + mime.getType() + "\" part.]");
  233.          } else {
  234.             String s = "";
  235.  
  236.             for(int i = 0; i < mime.getBodyLineCount(); ++i) {
  237.                s = s + "\n" + mime.getBodyLine(i);
  238.             }
  239.  
  240.             this.readScreen.addBody(s);
  241.          }
  242.       } else {
  243.          for(int p = 0; p < mime.getPartCount(); ++p) {
  244.             this.addPartToScreen(mime.getPart(p));
  245.          }
  246.       }
  247.  
  248.    }
  249. }
  250.