import java.net.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;

class EMailSender extends Frame{

// Version & Autor
private static final String VersionsDatum 	= "29.09.2004";
private static final String Autor		= "Manfred Sommer";

private static String Titel = "Erstellt und versendet ein Test EMail ";

// TCP/IP Felder
   
   private static int mailerPort = 25;
   private static InetAddress mailerIPAdr;
   private static Socket mailerSocket;

   private static int myPort = 2001;
   private static ServerSocket myServer;

// Fonts
    private static final Font FontMono           = new Font("Monospaced", Font.PLAIN, 18);
    private static final Font FontSansSerif 	 = new Font("SansSerif", Font.PLAIN, 14);
    private static final Font BigItalicSansSerif = new Font("SansSerif", Font.ITALIC, 16);

     
// Haupt - Fenstergrösse
    private static int XMax =  700;		
    private static int YMax =  750;
	

// Fenster für Meldungen 
    private static Label    Meld1Label = new Label("An Mail Server gesendet", Label.LEFT);
    private static TextArea Meldungen1 = new TextArea(5,55);
    private static Label    Meld2Label = new Label("Antworten vom Mail Server", Label.LEFT);
    private static TextArea Meldungen2 = new TextArea(5,55);

// Felder, Label und Fenster für Eingaben
    private static Label     EMailLabel = new Label("EMail Text", Label.LEFT);
    private static TextArea  EMailText  = new TextArea("Test Mail !", 5,55);
    private static Label     MailerLabel = new Label("Mailer", Label.LEFT);
    private static String    MailerText = "mailhost.mathematik.uni-marburg.de";
    private static TextField MailerFeld = new TextField(MailerText,55);
    private static Label     AbsenderLabel = new Label("Absender", Label.LEFT);
    private static String    AbsenderText = "sommer@informatik.uni-marburg.de";
    private static TextField AbsenderFeld = new TextField(AbsenderText,55);
    private static Label     EmpfaengerLabel = new Label("Empfaenger", Label.LEFT);
    private static String    EmpfaengerText = "sommer@informatik.uni-marburg.de";
    private static TextField EmpfaengerFeld = new TextField(EmpfaengerText,55);
       
	
// Beginn Menü 
  	
    private static MenuBar  MainMenuBar = new MenuBar();
    private static Menu     DateiMenu 	= new Menu("Datei");
    private static MenuItem DateiOpen	= new MenuItem("Öffnen...");
    private static Menu     BearbeitenMenu = new Menu("Bearbeiten");
    private static MenuItem SendMail  = new MenuItem("Mail Abschicken");

    private static MenuItem LoeMenu  = new MenuItem("Fenster löschen");
    private static MenuItem Beenden	= new MenuItem("Exit");
    private static Menu     HelpMenu 	= new Menu("Hilfe");
    private static MenuItem AboutBox	= new MenuItem("About");
        
// End Menü 

//// Konstruktor.
 
    EMailSender () {
	super(Titel);
			
	// Fonts für Menüs
	setFont(FontMono);
	DateiMenu.setFont(FontSansSerif);
	DateiOpen.setFont(FontSansSerif);
	BearbeitenMenu.setFont(FontSansSerif);
	SendMail.setFont(FontSansSerif);
	LoeMenu.setFont(FontSansSerif);
	Beenden.setFont(FontSansSerif);
	HelpMenu.setFont(FontSansSerif);
	AboutBox.setFont(FontSansSerif);
	// Font für Text Bereich
	setFont(new Font("Monospaced", Font.PLAIN, 18));
	
	setLayout(new FlowLayout(FlowLayout.LEADING));
	
	setMenuBar(MainMenuBar);
		
	MainMenuBar.add(DateiMenu);
	DateiMenu.add(DateiOpen);
	DateiMenu.add(Beenden);
	
	MainMenuBar.add(BearbeitenMenu);
	BearbeitenMenu.add(SendMail);
	BearbeitenMenu.add(LoeMenu);
		
	MainMenuBar.add(HelpMenu);
	HelpMenu.add(AboutBox);
	
	add(MailerLabel);
	add(MailerFeld);
	add(AbsenderLabel);
	add(AbsenderFeld);
	add(EmpfaengerLabel);
	add(EmpfaengerFeld);
	add(EMailLabel);
	add(EMailText);

	add(Meld1Label);
        Meldungen1.setEditable(false);
        add(Meldungen1);
        add(Meld2Label);
        Meldungen2.setEditable(false);
        add(Meldungen2);
               
        
        DateiOpen.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent e){ doOpenFile();}
	   });
	
	SendMail.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent e){ doSendMail();}
	    });	
	
	
	LoeMenu.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent e){ 
	    	Meldungen1.setText("");
	    	Meldungen2.setText("");
	    	}
	    });
	
		
	Beenden.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent e){ FensterBeenden(); }
		});
		

	AboutBox.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){ showAboutBox(); }
		});
	    
		
	addWindowListener(new WindowAdapter() {    
	        public void windowClosing(WindowEvent e) { FensterBeenden(); }
		});
		    
	setBackground(Color.lightGray); 
	setSize(XMax, YMax);   
	setVisible(true);
        }

  void FensterBeenden() {	
	        
                dispose();
                System.exit(0);
            }
            
  // show about box
	void showAboutBox(){
		final Frame 	f 	= new Frame("About Box");
		final Button 	Ok 	= new Button("Ok");
	
		f.addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent e) {
				f.dispose();
			}
		});
		    
		Ok.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e) {
				f.dispose();
			}
		});
		
		f.setFont	(BigItalicSansSerif);
		f.setLayout	(new GridLayout(0, 1));
	
		f.add(new Label(" ", Label.CENTER));
		f.add(new Label(Titel, Label.CENTER));
		f.add(new Label("Datum der letzten Änderung: " + VersionsDatum, Label.CENTER));
		f.add(new Label("Autor: Manfred Sommer", Label.CENTER));
		f.add(new Label(" ", Label.CENTER));
		
		f.add(Ok);
		f.pack(); 
		f.setVisible(true);
	}
	
	

  
    void doOpenFile(){
    Meldungen1.append("Dummy für: Datei - öffnen.\n");
    }    


  void doSendMail(){
    MailerText     = MailerFeld.getText();
    AbsenderText   = AbsenderFeld.getText();
    EmpfaengerText = EmpfaengerFeld.getText();
    String mailMessage = "From:" + AbsenderText + "\n";
    mailMessage += "To:" + EmpfaengerText + "\n";
  //  mailMessage += "Grober Unfung\n";
    mailMessage += "Cc:" + AbsenderText + "\n";
    mailMessage += "Subject:Dies ist eine Test EMail\n";
    mailMessage += " \n" + EMailText.getText() + "\n.\n";
    
    if (mailerIPAdr == null){
        //Meldungen1.append("IP Adresse von " + MailerText + " ermitteln...\n");
        try { mailerIPAdr = InetAddress.getByName(MailerText); }
        catch (UnknownHostException e) {
            Meldungen1.append("IP-Adresse des Mailers nicht gefunden.\n");
            return;
            }	
        }
    //Meldungen1.append("TCP Verbindung aufbauen...\n");
    mailerSocket = null;
    try { mailerSocket = new Socket(mailerIPAdr, mailerPort); }
    catch (IOException e) {
      Meldungen1.append("Konnte keine TCP Verbindung herstellen ... " + e + "\n");
      return;
      }
    //Meldungen1.append("TCP Verbindung steht!\n");
    PrintWriter SockeOut;
    BufferedReader SockeIn;
    try { // Input/Output Streams verbinden
        SockeOut = new PrintWriter(mailerSocket.getOutputStream(), true);
        SockeIn  = new BufferedReader(new InputStreamReader(mailerSocket.getInputStream()));
       } 
     catch (IOException e) {
        Meldungen2.append("Input/Output Streams verbinden - Fehler " + e +"\n");
        return;
	}
     
     String Anfrage;
     if (!toFromServer(null, SockeOut, SockeIn)) return;
     Anfrage = "HELO sommer";
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     Anfrage = "MAIL From:<" + AbsenderText + ">";
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     Anfrage = "RCPT To:<" + EmpfaengerText + ">";
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     Anfrage = "DATA";
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     Anfrage = mailMessage;
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     Anfrage = "QUIT";
     if (!toFromServer(Anfrage, SockeOut, SockeIn)) return;
     try {
     	SockeOut.close(); SockeOut = null;
     	SockeIn.close();  SockeIn  = null;
     	mailerSocket.close(); mailerSocket = null;
        } 
     catch(IOException e) {}
    }

 
  private static boolean toFromServer(String Anfrage, PrintWriter toServer, BufferedReader fromServer){
     if (Anfrage != null){
     	Meldungen1.append(Anfrage + "\n");
        toServer.println(Anfrage);
        }
     String Gelesen = null;
     try{
       while (!fromServer.ready());
       while (fromServer.ready()) 
         Gelesen = fromServer.readLine();
         if (Gelesen != null) Meldungen2.append(Gelesen + "\n");
       }
     catch (IOException e) { 
     	Meldungen2.append("Server - Lese Fehler" + e + "\n");
     	return false;
     	}     
     return true;
   }  

  public static void main(String args[]) {
    new EMailSender();
    }

   

}// class EMailSender
	
 