org/sonews/mlgw/SMTPTransport.java
author chris <chris@marvin>
Wed Jul 22 14:04:05 2009 +0200 (2009-07-22)
changeset 3 2fdc9cc89502
child 12 bb6990c0dd1a
permissions -rw-r--r--
sonews/1.0.0
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.mlgw;
    20 
    21 import java.io.BufferedOutputStream;
    22 import java.io.BufferedReader;
    23 import java.io.FileOutputStream;
    24 import java.io.IOException;
    25 import java.io.InputStreamReader;
    26 import java.io.PrintWriter;
    27 import java.net.Socket;
    28 import java.net.UnknownHostException;
    29 import org.sonews.config.Config;
    30 import org.sonews.storage.Article;
    31 import org.sonews.util.io.ArticleInputStream;
    32 
    33 /**
    34  * Connects to a SMTP server and sends a given Article to it.
    35  * @author Christian Lins
    36  */
    37 class SMTPTransport
    38 {
    39 
    40   protected BufferedReader in;
    41   protected PrintWriter    out;
    42   protected Socket         socket;
    43 
    44   public SMTPTransport(String host, int port)
    45     throws IOException, UnknownHostException
    46   {
    47     socket = new Socket(host, port);
    48     this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    49     this.out = new PrintWriter(socket.getOutputStream());
    50 
    51     // Read helo from server
    52     String line = this.in.readLine();
    53     if(line == null || !line.startsWith("220 "))
    54     {
    55       throw new IOException("Invalid helo from server: " + line);
    56     }
    57 
    58     // Send HELO to server
    59     this.out.println("HELO " + Config.inst().get(Config.HOSTNAME, "localhost"));
    60     this.out.flush();
    61     line = this.in.readLine();
    62     if(line == null || !line.startsWith("250 "))
    63     {
    64       throw new IOException("Unexpected reply: " + line);
    65     }
    66   }
    67 
    68   public SMTPTransport(String host)
    69     throws IOException
    70   {
    71     this(host, 25);
    72   }
    73 
    74   public void close()
    75     throws IOException
    76   {
    77     this.out.println("QUIT");
    78     this.out.flush();
    79     this.in.readLine();
    80 
    81     this.socket.close();
    82   }
    83 
    84   public void send(Article article, String mailFrom, String rcptTo)
    85     throws IOException
    86   {
    87     this.out.println("MAIL FROM: " + mailFrom);
    88     this.out.flush();
    89     String line = this.in.readLine();
    90     if(line == null || !line.startsWith("250 "))
    91     {
    92       throw new IOException("Unexpected reply: " + line);
    93     }
    94 
    95     this.out.println("RCPT TO: " + rcptTo);
    96     this.out.flush();
    97     line  = this.in.readLine();
    98     if(line == null || !line.startsWith("250 "))
    99     {
   100       throw new IOException("Unexpected reply: " + line);
   101     }
   102 
   103     this.out.println("DATA");
   104     this.out.flush();
   105     line = this.in.readLine();
   106     if(line == null || !line.startsWith("354 "))
   107     {
   108       throw new IOException("Unexpected reply: " + line);
   109     }
   110 
   111     ArticleInputStream   artStream = new ArticleInputStream(article);
   112     BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
   113     FileOutputStream     fileStream = new FileOutputStream("smtp.dump");
   114     for(int b = artStream.read(); b >= 0; b = artStream.read())
   115     {
   116       outStream.write(b);
   117       fileStream.write(b);
   118     }
   119 
   120     // Flush the binary stream; important because otherwise the output
   121     // will be mixed with the PrintWriter.
   122     outStream.flush();
   123     fileStream.flush();
   124     fileStream.close();
   125     this.out.print("\r\n.\r\n");
   126     this.out.flush();
   127     line = this.in.readLine();
   128     if(line == null || !line.startsWith("250 "))
   129     {
   130       throw new IOException("Unexpected reply: " + line);
   131     }
   132   }
   133 
   134 }