
package CEO_OM; 


import  java.io.*;
import  java.util.*;
import  java.net.*;
import  java.lang.*;

import spider.*;		
import spider.access.*;	
import spider.util.*;

// This file has been generated by NetDynamics Studio
// ++
//
//  Web Page:		SMPTMailer
//
//	Description:    
//		Simple class which supports sending e-mail from a NetDynamics app.
//		This routine is used in conjunction with the file SMPTMailer.ini
//
//	Usage - instantiate a Mailer object.
//		Invoke its sendMsg() function as often as you want.
// 
//  Improvements needed -
//		More error checking.
//		Handling of recipient list for broadcasting a message.
//
//	On Constructors -
//		Two construcors. One allows you to specify a properties file which contain
//		the mailserver, portnumber and sender name.
//		The other constructor allows you to pass in the same values programattically.
//
//	On succeeded() -
//		Check succeeded after construction and after message sending to make sure
//		that all went well.
//		 
//  Revision: 
//	1998		Jack 		Not Tracked
//
// --

import spider.session.*;	
import spider.html.*;


public class SMTPMailer
{
    private static Properties _props = new Properties();

    private static String _localhost;

    private static String _mailserver = "";
    private static int    _mailport   = 25; // default for mailserver
    private static String _sender;
    private static String _receiver;
    private static String _subject;


    private static Socket          _socket;
    private static DataInputStream _inStream;
    private static PrintStream     _outStream;

    private static boolean _succeeded = true;

    // register this class with and provide its name to the logging util
    private static int _logId = CSpLog.registerClass ("SMTPMailer");



    // ** This is obsolete. Must be updated to use **
    // **
    // use this constructor if you are NOT supplying an ini file.
    public SMTPMailer(String mailserver, int port, String sender) 
    {
        _mailserver = mailserver;
        _sender     = sender;
        _mailport   = port;

        // determine local _localhost from system
        try {
            InetAddress ipaddress = InetAddress.getLocalHost();
            if ( (_localhost = ipaddress.getHostName()) == null )
                _localhost = "localhost";
        } 
        catch (UnknownHostException ex) {
            _localhost = "localhost";
        }

    }



    // This constructor is used when you supply an ini file.
    // It assumes that the .ini file is in the same directory as the project
    //
    public SMTPMailer(String iniFile) 
    {
        _succeeded = true; 

        // get key settings from the property file
        _props = new Properties();

        String basename = new String();
		
        basename = CSpider.getEnvVar(CSpVars.NETDYN_HOME) + File.separator +
                   CSpider.getDefaultProjectPath().getFullQualifiedPath() + 
                   File.separator ;

        iniFile = basename + iniFile ;
		
        //CSpHtml.sendMessage(CSpider.getOutputStream(),".ini file is " + iniFile + "<br>" );

        try {
            _props.load(new FileInputStream(iniFile));
        } 
        catch ( IOException ex) {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): IO-Can't Read file " 
                         + iniFile + "(" + ex + ")");
            _succeeded = false;
        }

        // check, if required properties are set.
        if ( (_mailserver = _props.getProperty("MAILSERVER")) == null) 
        {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): MAILSERVER not specified");
            _succeeded = false;
        }	
				
        if ( (_sender = _props.getProperty("SENDER")) == null)
        {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): SENDER not specified");
            _succeeded = false;
        }

        String temp;
        try {
            _mailport = (new Integer((((temp = _props.getProperty("MAILPORT")) == null)
                        ? "25" : temp))).intValue();
        } 
        catch (NumberFormatException ex )
        {
            // stick w/ default port and continue ...
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): MAILPORT wrong format - continuing with default port" );
        }

        if ( (_receiver = _props.getProperty("RECEIVER")) == null)
        {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): RECEIVER not specified");
            _succeeded = false;
        }

        if ( (_subject = _props.getProperty("SUBJECT")) == null)
        {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer(): SUBJECT not specified");
            _succeeded = false;
        }

        // determine local _localhost from system
        try {
            InetAddress ipaddress = InetAddress.getLocalHost();
            if ( (_localhost = ipaddress.getHostName()) == null )
                _localhost = "localhost";
        } 
        catch (UnknownHostException ex)
        {
            _localhost = "localhost";
        }

    } // End public SMTPMailer 



    public boolean succeeded() {
        return _succeeded;
    } 


    // public void sendMsg (String recipient, String subject, String msg ) 
    public void sendMsg ( String msg ) 
    {
        // possible but not likely we could succeed on different occasions
        _succeeded = true; 

        String reply;

        if( _subject.length() > 0 )
            msg = "Subject: " + _subject + "\n" + msg;
		
        try
        {
            _socket    = new Socket(_mailserver, _mailport);	
            _inStream  = new DataInputStream( _socket.getInputStream() );
            _outStream = new PrintStream(_socket.getOutputStream() );
		
            reply = _inStream.readLine();
            CSpLog.send (_logId, CSpLog.MEDIUM_LEVEL,
                         "Mailer.sendMsg() " + reply );
		
            lineout("HELO " + _localhost, true );
            lineout("MAIL FROM: " + _sender, true );
            lineout("RCPT TO: " + _receiver, true );
            lineout("DATA", true);
            lineout( msg, false);
            lineout( "\n.\n", false);

            CSpLog.send (_logId, CSpLog.MEDIUM_LEVEL,
                         "Mailer.sendMsg() mail sent to " + _receiver );
        }
        catch(IOException ex)
         {
            CSpLog.send (_logId, CSpLog.ERROR,
                         "Mailer.sendMsg: Socket IO Error " + ex);
            _succeeded = false;
        }

        // This is temporary until we link into new pages!
	/*
        if(_succeeded)
            CSpHtml.sendMessage(CSpider.getOutputStream(),"Message Sent successfully <br>" );
        else
            CSpHtml.sendMessage(CSpider.getOutputStream(),"Message Failed <br>" );
	*/	
    }


    private static void lineout(String outline, boolean hasReply) 
    {

        String reply = new String();
		
        try
        {
            _outStream.println( outline);
            _outStream.flush();
            if ( hasReply )
            {
                CSpLog.send (_logId, CSpLog.MEDIUM_LEVEL,
                             "Mailer.lineout() " + _inStream.readLine());
            }
        }
        catch(IOException ex)
        {
            _outStream.println( "\n.\n" );
            CSpLog.send (_logId, CSpLog.ERROR, "Mailer.lineout() " + ex );
            _succeeded = false;
        }
        return;
    }

}

