home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / smtp / SmtpClient.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  5.2 KB  |  215 lines

  1. /*
  2.  * @(#)SmtpClient.java    1.9 95/08/29 James Gosling
  3.  * 
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  8.  * granted provided that this copyright notice appears in all copies. Please
  9.  * refer to the file "copyright.html" for further important copyright and
  10.  * licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  15.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  16.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  17.  * ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net.smtp;
  21.  
  22. import java.util.StringTokenizer;
  23. import java.io.*;
  24. import java.net.*;
  25. import sun.net.TransferProtocolClient;
  26.  
  27. /**
  28.  * This class implements the SMTP client.
  29.  * You can send a piece of mail by creating a new SmtpClient, calling
  30.  * the "to" method to add destinations, calling "from" to name the
  31.  * sender, calling startMessage to return a stream to which you write
  32.  * the message (with RFC733 headers) and then you finally close the Smtp
  33.  * Client.
  34.  *
  35.  * @version    1.17, 12 Dec 1994
  36.  * @author    James Gosling
  37.  */
  38.  
  39. public class SmtpClient extends TransferProtocolClient {
  40.     SmtpPrintStream message;
  41.  
  42.     /**
  43.      * issue the QUIT command to the SMTP server and close the connection.
  44.      */
  45.     public void closeServer() throws IOException {
  46.     if (serverIsOpen()) {
  47.         closeMessage();
  48.         issueCommand("QUIT\r\n", 221);
  49.         super.closeServer();
  50.     }
  51.     }
  52.  
  53.     void issueCommand(String cmd, int expect) throws IOException {
  54.     sendServer(cmd);
  55.     int reply;
  56.     while ((reply = readServerResponse()) != expect)
  57.         if (reply != 220) {
  58.         throw new SmtpProtocolException(getResponseString());
  59.         }
  60.     }
  61.  
  62.     private void toCanonical(String s) throws IOException {
  63.     issueCommand("rcpt to: " + s + "\r\n", 250);
  64.     }
  65.  
  66.     public void to(String s) throws IOException {
  67.     int st = 0;
  68.     int limit = s.length();
  69.     int pos = 0;
  70.     int lastnonsp = 0;
  71.     int parendepth = 0;
  72.     boolean ignore = false;
  73.     while (pos < limit) {
  74.         int c = s.charAt(pos);
  75.         if (parendepth > 0) {
  76.         if (c == '(')
  77.             parendepth++;
  78.         else if (c == ')')
  79.             parendepth--;
  80.         if (parendepth == 0)
  81.             if (lastnonsp > st)
  82.             ignore = true;
  83.             else
  84.             st = pos + 1;
  85.         } else if (c == '(')
  86.         parendepth++;
  87.         else if (c == '<')
  88.         st = lastnonsp = pos + 1;
  89.         else if (c == '>')
  90.         ignore = true;
  91.         else if (c == ',') {
  92.         if (lastnonsp > st)
  93.             toCanonical(s.substring(st, lastnonsp));
  94.         st = pos + 1;
  95.         ignore = false;
  96.         } else {
  97.         if (c > ' ' && !ignore)
  98.             lastnonsp = pos + 1;
  99.         else if (st == pos)
  100.             st++;
  101.         }
  102.         pos++;
  103.     }
  104.     if (lastnonsp > st)
  105.         toCanonical(s.substring(st, lastnonsp));
  106.     }
  107.  
  108.     public void from(String s) throws IOException {
  109.     issueCommand("mail from: " + s + "\r\n", 250);
  110.     }
  111.  
  112.     /** open a SMTP connection to host <i>host</i>. */
  113.     private void openServer(String host) throws IOException {
  114.     openServer(host, 25);
  115.     issueCommand("helo "+InetAddress.getLocalHost().getHostName()+"\r\n", 250);
  116.     }
  117.  
  118.     public PrintStream startMessage() throws IOException {
  119.     issueCommand("data\r\n", 354);
  120.     return message = new SmtpPrintStream(serverOutput, this);
  121.     }
  122.  
  123.     void closeMessage() throws IOException {
  124.     if (message != null)
  125.         message.close();
  126.     }
  127.  
  128.     /** New SMTP client connected to host <i>host</i>. */
  129.     public SmtpClient (String host) throws IOException {
  130.     super();
  131.     if (host != null) {
  132.         try {
  133.         openServer(host);
  134.         return;
  135.         } catch(Exception e) {
  136.         }
  137.     }
  138.     try {
  139.         String s = System.getProperty("mail.host");
  140.         if (s != null) {
  141.         openServer(s);
  142.         return;
  143.         }
  144.     } catch(Exception e) {
  145.     }
  146.     try {
  147.         openServer("localhost");
  148.     } catch(Exception e) {
  149.         openServer("mailhost");
  150.     }
  151.     }
  152.  
  153.     /** Create an uninitialized SMTP client. */
  154.     public SmtpClient () throws IOException {
  155.     this(null);
  156.     }
  157. }
  158.  
  159. class SmtpPrintStream extends java.io.PrintStream {
  160.     private SmtpClient target;
  161.     private int lastc = '\n';
  162.  
  163.     SmtpPrintStream (OutputStream fos, SmtpClient cl) {
  164.     super(fos);
  165.     target = cl;
  166.     }
  167.  
  168.     public void close() {
  169.     if (target == null)
  170.         return;
  171.     if (lastc != '\n') {
  172.         write('\r');
  173.         write('\n');
  174.     }
  175.     try {
  176.         target.issueCommand(".\r\n", 250);
  177.         target.message = null;
  178.         out = null;
  179.         target = null;
  180.     } catch (IOException e) {
  181.     }
  182.     }
  183.  
  184.     public void write(int b) {
  185.     try {
  186.         if (lastc == '\n' && b == '.')
  187.         out.write('.');
  188.         out.write(b);
  189.         lastc = b;
  190.     } catch (IOException e) {
  191.     }
  192.     }
  193.  
  194.     public void write(byte b[], int off, int len) {
  195.     try {
  196.         int lc = lastc;
  197.         while (--len >= 0) {
  198.         int c = b[off++];
  199.         if (lc == '\n' && c == '.')
  200.             out.write('.');
  201.         out.write(c);
  202.         lc = c;
  203.         }
  204.         lastc = lc;
  205.     } catch (IOException e) {
  206.     }
  207.     }
  208.     public void print(String s) {
  209.     int len = s.length();
  210.     for (int i = 0; i < len; i++) {
  211.         write(s.charAt(i));
  212.     }
  213.     }
  214. }
  215.