<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">// ##########################################################################       
// ####                                                                  ####
// ####                  Publikations-Manager Komponenten                ####
// ####                ====================================              ####
// ####                                                                  ####
// #### MailClient.java                                                  ####
// #### TCP/IP-Kommunikation mit einem SMTP-Server nach RFC 821          ####
// ####                                                                  ####
// #### Version 1.00, 19. Dezember 1999                                  ####
// ####                                                                  ####
// #### Copyright (C) 1999  Thomas Dreibholz                             ####
// ####               2000  Universität Bonn                             ####
// ####               EMail: dreibh@exp-math.uni-essen.de                ####
// ####               WWW:   http://www.exp-math.uni-essen.de/~dreibh    ####
// ####                                                                  ####
// ####                                                                  ####
// ##########################################################################


import java.io.*;
import java.net.*;


// ****** SMTP-Client ****************************************************
public class MailClient
{
   final boolean debug = true;


   // ###### Interne Variablen für TCP/IP-Kommunikation ##################
   private Socket socket          = null;
   private BufferedReader in      = null;
   private PrintWriter    out     = null;       
   private String         antwort = null;


   // ###### Konstruktor #################################################
   // serverName = Name des SMTP-Servers
   // serverPort = TCP-Port des SMTP-Servers; Standard: 25
   public MailClient(String serverName, int serverPort) throws IOException {
      socket = new Socket(serverName,serverPort);
      in     = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      out    = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
      int ok = LeseAntwort();            
      if(ok != 220) {
         throw new IOException(antwort);
      }
   }
   
   
   // ###### Antwort vom Server lesen, als Integer-Wert zurückgeben ######     
   protected int LeseAntwort() throws IOException {
      // Antwort vom Server lesen; bei mehrzeiliger Antwort bis zum Ende lesen
      do {
         antwort = in.readLine();               
         if(debug) System.out.println(antwort);         
      } while(antwort.charAt(3) == '-');

      // Antwort in Integer umwandeln      
      Integer i = new Integer(antwort.substring(0,3));
      return(i.intValue());
   }


   // ###### Letzte Antwort vom Server zurückgeben (für Debugging) #######
   public String LetzteAntwort() {
      return(antwort);
   }
   

   // ###### Hello-Befehl; siehe RFC 821 #################################   
   public boolean Hello(String domain) throws IOException {
      out.println("HELO " + domain);      
      return(LeseAntwort() == 250);
   }


   // ###### Mail From-Befehl; siehe RFC 821 #############################
   public boolean MailFrom(String name) throws IOException {
      out.println("MAIL FROM: &lt;" + name + "&gt;");      
      return(LeseAntwort() == 250);
   }


   // ###### Rcpt To-Befehl; siehe RFC 821 ###############################
   public boolean RcptTo(String name) throws IOException {
      out.println("RCPT TO: &lt;" + name + "&gt;");
      int i = LeseAntwort();
      return((i == 250) || (i == 251));
   }


   // ###### Data-Befehl; siehe RFC 821 ##################################
   public boolean Data(String text) throws IOException {
      out.println("DATA");      
      if(LeseAntwort() == 354) {
         out.println(text + "\n.");
         return(LeseAntwort() == 250);
      }
      return(false);
   }

 
   // ###### Quit-Befehl; siehe RFC 821 ##################################
   public boolean Quit() throws IOException {
      out.println("QUIT");      
      return(LeseAntwort() == 221);            
   }
}
</pre></body></html>