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 / SmtpClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-10-21  |  4.3 KB  |  130 lines

  1. package de.trantor.mail;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Vector;
  7.  
  8. public class SmtpClient {
  9.    private String host;
  10.    private Connection socket;
  11.    private InputStream input;
  12.    private OutputStream output;
  13.    private boolean debug = false;
  14.    private String localhost;
  15.  
  16.    public SmtpClient(String localhost) {
  17.       this.localhost = localhost;
  18.    }
  19.  
  20.    public void open(String host) throws Exception, IOException, ClassNotFoundException, SmtpException {
  21.       if (this.connected()) {
  22.          this.close();
  23.       }
  24.  
  25.       this.socket = Connection.createConnection();
  26.       this.socket.open(host, 25);
  27.  
  28.       try {
  29.          this.input = this.socket.getInputStream();
  30.          this.output = this.socket.getOutputStream();
  31.          this.receive();
  32.          this.execute("HELO " + this.localhost);
  33.       } catch (SmtpException var3) {
  34.          this.socket.close();
  35.          this.socket = null;
  36.          this.input = null;
  37.          this.output = null;
  38.          throw var3;
  39.       }
  40.    }
  41.  
  42.    public void close() throws IOException, SmtpException {
  43.       if (this.connected()) {
  44.          this.execute("QUIT");
  45.       }
  46.  
  47.       this.socket.close();
  48.       this.input = null;
  49.       this.output = null;
  50.       this.socket = null;
  51.    }
  52.  
  53.    public boolean connected() {
  54.       return this.socket != null;
  55.    }
  56.  
  57.    private void send(String s) throws IOException, SmtpException {
  58.       if (this.debug) {
  59.          System.out.println("[SMTP/SEND] " + s);
  60.       }
  61.  
  62.       this.output.write(s.getBytes());
  63.       this.output.write(13);
  64.       this.output.write(10);
  65.    }
  66.  
  67.    private String receive() throws IOException, SmtpException {
  68.       StringBuffer buffer = new StringBuffer("");
  69.  
  70.       char c;
  71.       do {
  72.          c = (char)this.input.read();
  73.          if (c != -1 && c != '\r' && c != '\n') {
  74.             buffer.append(c);
  75.          }
  76.       } while(c != '\r');
  77.  
  78.       if (this.debug) {
  79.          System.out.println("[SMTP/RECV] " + buffer);
  80.       }
  81.  
  82.       return new String(buffer);
  83.    }
  84.  
  85.    private String execute(String command) throws IOException, SmtpException {
  86.       this.send(command);
  87.       String result = this.receive();
  88.       char c = result.charAt(0);
  89.       if (c != '4' && c != '5') {
  90.          return result;
  91.       } else {
  92.          throw new SmtpException(result);
  93.       }
  94.    }
  95.  
  96.    public void sendMessage(Message message) throws IOException, SmtpException {
  97.       this.sendMessage(new Envelope(message, true));
  98.    }
  99.  
  100.    public void sendMessage(Envelope envelope) throws IOException, SmtpException {
  101.       this.execute("MAIL FROM:" + envelope.getSender());
  102.  
  103.       for(int i = 0; i < envelope.getRecipientCount(); ++i) {
  104.          this.execute("RCPT TO:" + envelope.getRecipient(i));
  105.       }
  106.  
  107.       this.execute("DATA");
  108.       Vector lines = envelope.getMessage().getLines();
  109.  
  110.       for(int i = 0; i < lines.size(); ++i) {
  111.          String s = (String)lines.elementAt(i);
  112.          if (s.length() != 0 && s.charAt(0) == '.') {
  113.             s = '.' + s;
  114.          }
  115.  
  116.          this.send(s);
  117.       }
  118.  
  119.       this.execute(".");
  120.    }
  121.  
  122.    public boolean getDebug() {
  123.       return this.debug;
  124.    }
  125.  
  126.    public void setDebug(boolean value) {
  127.       this.debug = value;
  128.    }
  129. }
  130.