org/sonews/mlgw/SMTPTransport.java
changeset 3 2fdc9cc89502
child 12 bb6990c0dd1a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/mlgw/SMTPTransport.java	Wed Jul 22 14:04:05 2009 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.mlgw;
    1.23 +
    1.24 +import java.io.BufferedOutputStream;
    1.25 +import java.io.BufferedReader;
    1.26 +import java.io.FileOutputStream;
    1.27 +import java.io.IOException;
    1.28 +import java.io.InputStreamReader;
    1.29 +import java.io.PrintWriter;
    1.30 +import java.net.Socket;
    1.31 +import java.net.UnknownHostException;
    1.32 +import org.sonews.config.Config;
    1.33 +import org.sonews.storage.Article;
    1.34 +import org.sonews.util.io.ArticleInputStream;
    1.35 +
    1.36 +/**
    1.37 + * Connects to a SMTP server and sends a given Article to it.
    1.38 + * @author Christian Lins
    1.39 + */
    1.40 +class SMTPTransport
    1.41 +{
    1.42 +
    1.43 +  protected BufferedReader in;
    1.44 +  protected PrintWriter    out;
    1.45 +  protected Socket         socket;
    1.46 +
    1.47 +  public SMTPTransport(String host, int port)
    1.48 +    throws IOException, UnknownHostException
    1.49 +  {
    1.50 +    socket = new Socket(host, port);
    1.51 +    this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.52 +    this.out = new PrintWriter(socket.getOutputStream());
    1.53 +
    1.54 +    // Read helo from server
    1.55 +    String line = this.in.readLine();
    1.56 +    if(line == null || !line.startsWith("220 "))
    1.57 +    {
    1.58 +      throw new IOException("Invalid helo from server: " + line);
    1.59 +    }
    1.60 +
    1.61 +    // Send HELO to server
    1.62 +    this.out.println("HELO " + Config.inst().get(Config.HOSTNAME, "localhost"));
    1.63 +    this.out.flush();
    1.64 +    line = this.in.readLine();
    1.65 +    if(line == null || !line.startsWith("250 "))
    1.66 +    {
    1.67 +      throw new IOException("Unexpected reply: " + line);
    1.68 +    }
    1.69 +  }
    1.70 +
    1.71 +  public SMTPTransport(String host)
    1.72 +    throws IOException
    1.73 +  {
    1.74 +    this(host, 25);
    1.75 +  }
    1.76 +
    1.77 +  public void close()
    1.78 +    throws IOException
    1.79 +  {
    1.80 +    this.out.println("QUIT");
    1.81 +    this.out.flush();
    1.82 +    this.in.readLine();
    1.83 +
    1.84 +    this.socket.close();
    1.85 +  }
    1.86 +
    1.87 +  public void send(Article article, String mailFrom, String rcptTo)
    1.88 +    throws IOException
    1.89 +  {
    1.90 +    this.out.println("MAIL FROM: " + mailFrom);
    1.91 +    this.out.flush();
    1.92 +    String line = this.in.readLine();
    1.93 +    if(line == null || !line.startsWith("250 "))
    1.94 +    {
    1.95 +      throw new IOException("Unexpected reply: " + line);
    1.96 +    }
    1.97 +
    1.98 +    this.out.println("RCPT TO: " + rcptTo);
    1.99 +    this.out.flush();
   1.100 +    line  = this.in.readLine();
   1.101 +    if(line == null || !line.startsWith("250 "))
   1.102 +    {
   1.103 +      throw new IOException("Unexpected reply: " + line);
   1.104 +    }
   1.105 +
   1.106 +    this.out.println("DATA");
   1.107 +    this.out.flush();
   1.108 +    line = this.in.readLine();
   1.109 +    if(line == null || !line.startsWith("354 "))
   1.110 +    {
   1.111 +      throw new IOException("Unexpected reply: " + line);
   1.112 +    }
   1.113 +
   1.114 +    ArticleInputStream   artStream = new ArticleInputStream(article);
   1.115 +    BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
   1.116 +    FileOutputStream     fileStream = new FileOutputStream("smtp.dump");
   1.117 +    for(int b = artStream.read(); b >= 0; b = artStream.read())
   1.118 +    {
   1.119 +      outStream.write(b);
   1.120 +      fileStream.write(b);
   1.121 +    }
   1.122 +
   1.123 +    // Flush the binary stream; important because otherwise the output
   1.124 +    // will be mixed with the PrintWriter.
   1.125 +    outStream.flush();
   1.126 +    fileStream.flush();
   1.127 +    fileStream.close();
   1.128 +    this.out.print("\r\n.\r\n");
   1.129 +    this.out.flush();
   1.130 +    line = this.in.readLine();
   1.131 +    if(line == null || !line.startsWith("250 "))
   1.132 +    {
   1.133 +      throw new IOException("Unexpected reply: " + line);
   1.134 +    }
   1.135 +  }
   1.136 +
   1.137 +}