Noel - Thanks for the reply.  Please see my comments below.

> On May 18, 2015, at 8:58 PM, Noel Jones <[email protected]> wrote:
> 
> On 5/18/2015 7:12 PM, Robert Morton wrote:
>> I am trying to make a postfix after-que content filter with a java
>> program.  I am having trouble reading the message as it comes into
>> postfix with the java program I wrote.  My java program listens on
>> port 10025.  When a new message comes into postfix, the java program
>> successfully is “triggered/runs” and accepts the socket connection. 
>> The problem is that I cannot read an input stream to the socket.  I
>> would like to read the entirety of the e-mail message so I can
>> filter parts of its content and make subsequent decisions before
>> passing the filtered message back into postfix via port 10026.   The
>> java input stream simply does not have any data to read.  I then
>> terminate the java program, and I receive a “lost connection with
>> 127.0.0.1[127.0.0.1] while receiving the initial server” error
>> message via the “mailq” command.  The new mail stays in my que.
>> 
>> Possible Problems (1 to 3):  
>> 
>> 1) I must write to the socket with an output stream.  I would need
>> to send appropriate SMTP traffic such as the example below.  I tried
>> writing a “220 smtp.server.com <http://smtp.server.com>” Simple Mail
>> Transfer Service Ready” command.  Despite anything I send, I cannot
>> read any data from the java socket even though a connection is made.
> 
> If you configure postfix to use an SMTP content_filter, your program
> *must* speak SMTP; apparently it doesn’t.


When I write to the socket, I do not receive anything back.  How can I see that 
I am “speaking SMTP”, if I cannot receive any input from the java input-stream?
I know java programming is a little off topic.  I have included my code here.

—Begin Java Code---
public class DocRemoverContentFilter extends Thread
{
                private static boolean documentAttachment;
                private static InputStream is = null;
                private static String DocRemoverContentFilterOperatingSystem = 
null;
                private static String DocRemoverContentFilterMACAddress = null;
                private static String DocRemoverContentFilterEmailAddress = 
null;
                private static ArrayList<Integer> attachments = new 
ArrayList<>();
                private static ArrayList<String> attachmentURLList = new 
ArrayList<>();
                private static Part EmailWithOutAttachments = null;
                
                
                //Set Mail Properties to True for SSL
                public static void main(String[] args) throws Exception 
                {
                         try 
                         {
                                
DocRemoverContentFilter.receiveUnfilteredEmail();
                        } 
                        catch (Exception ioe) 
                        {
                                ioe.printStackTrace();
                        }
                        
                }

private static String receiveUnfilteredEmail() throws MessagingException
{

        System.out.println("Listening On Local Host On Port 10025...");
        
        try 
        {
                
                ServerSocket newServerSocket = new ServerSocket();
        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        
        
        SSLServerSocketFactory server = 
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
        SSLServerSocket ss = null;
        OutputStream out = null;
        InputStream in = null;
        
        try 
        {
                ss = (SSLServerSocket) server.createServerSocket(10025);
        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        
        SSLSocket socket = null;
        try 
        {
                socket = (SSLSocket) ss.accept(); //The new postfix message 
triggers this line to be successfully executed, so I know a connection is made.
                System.out.println("Accepted Connection...");
                out = socket.getOutputStream(); 
                out.flush(); //This helps flush the stream to ensure you can 
write to it.
                System.out.println("Flushing Output Stream...");
                in = socket.getInputStream();
                System.out.println("Setting Up Input Stream To Socket...");
                
        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        
        
        try 
        {
                System.out.println("Read next byte: " + in.read()); //This line 
just hangs.  I cannot read any information as if the socket is waiting.
        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        
        try 
        {
                ss.close(); //This closes the socket
        } 
        catch (IOException e) 
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

    return "";
}
}

——End of Java Code——



> 
>> 
>> 2) I do use TLS/SSL for my SMTP server.  I may need to implement the
>> certificates/key files into my java program.  I would like to get an
>> answer if the smtp(8) decrypts the sent traffic before sending it to
>> the content filter?  Please let me know.
> 
> TLS will not be used between postfix and the content_filter unless
> you specifically enable it.

I did enable TLS for postfix.  Does that mean that message is encrypted from 
smtp(8) to the content filter?



> 
>> 
>> 3) I am missing how to “communicate” with the smtp(8) or local(8)
>> that sends the e-mail message to my java program that listens on
>> port 10025.  Can I do with this socket programming and then can java
>> open an input stream to read the e-mail message?
> 
> You should be able to find some example java SMTP servers on the
> web.  Java programming details are off-topic here.

Thanks.  I did write a socket to be accepted, but it doesn’t seem to work.  I 
know java is a bit off topic, but I don’t know where else to turn.

> 
> 
>> 
>> ——Mail Que Error Message————
>> 
>> 
>> -Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
>> 0B3F76AC54D6B     1027 Thu May 14 17:52:22  [email protected]
>> <mailto:[email protected]>
>> (lost connection with 127.0.0.1[127.0.0.1] while receiving the
>> initial server greeting)
>> 
>> 
>> ——End of Mail Que Error Message————
> 
> 
> This is a normal error when the other end of the conversation does
> not support SMTP.
> 
> 
> 
> 
>  -- Noel Jones

Reply via email to