chris@3: /* chris@3: * SONEWS News Server chris@3: * see AUTHORS for the list of contributors chris@3: * chris@3: * This program is free software: you can redistribute it and/or modify chris@3: * it under the terms of the GNU General Public License as published by chris@3: * the Free Software Foundation, either version 3 of the License, or chris@3: * (at your option) any later version. chris@3: * chris@3: * This program is distributed in the hope that it will be useful, chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@3: * GNU General Public License for more details. chris@3: * chris@3: * You should have received a copy of the GNU General Public License chris@3: * along with this program. If not, see . chris@3: */ chris@3: chris@3: package org.sonews.mlgw; chris@3: chris@3: import java.io.BufferedOutputStream; chris@3: import java.io.BufferedReader; chris@3: import java.io.IOException; chris@3: import java.io.InputStreamReader; chris@3: import java.net.Socket; chris@3: import java.net.UnknownHostException; chris@3: import org.sonews.config.Config; chris@3: import org.sonews.storage.Article; chris@3: import org.sonews.util.io.ArticleInputStream; chris@3: chris@3: /** chris@3: * Connects to a SMTP server and sends a given Article to it. chris@3: * @author Christian Lins cli@28: * @since sonews/1.0 chris@3: */ chris@3: class SMTPTransport chris@3: { chris@3: cli@37: protected BufferedReader in; cli@37: protected BufferedOutputStream out; cli@37: protected Socket socket; chris@3: cli@37: public SMTPTransport(String host, int port) cli@37: throws IOException, UnknownHostException cli@37: { cli@37: socket = new Socket(host, port); cli@37: this.in = new BufferedReader(new InputStreamReader(socket.getInputStream())); cli@37: this.out = new BufferedOutputStream(socket.getOutputStream()); chris@3: cli@37: // Read helo from server cli@37: String line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("220 ")) { cli@37: throw new IOException("Invalid helo from server: " + line); cli@37: } chris@3: cli@37: // Send HELO to server cli@37: this.out.write( cli@37: ("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("250 ")) { cli@37: throw new IOException("Unexpected reply: " + line); cli@37: } cli@37: } chris@3: cli@37: public SMTPTransport(String host) cli@37: throws IOException cli@37: { cli@37: this(host, 25); cli@37: } chris@3: cli@37: public void close() cli@37: throws IOException cli@37: { cli@37: this.out.write("QUIT".getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: this.in.readLine(); chris@3: cli@37: this.socket.close(); cli@37: } chris@3: cli@37: public void send(Article article, String mailFrom, String rcptTo) cli@37: throws IOException cli@37: { cli@37: assert (article != null); cli@37: assert (mailFrom != null); cli@37: assert (rcptTo != null); cli@12: cli@37: this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: String line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("250 ")) { cli@37: throw new IOException("Unexpected reply: " + line); cli@37: } chris@3: cli@37: this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("250 ")) { cli@37: throw new IOException("Unexpected reply: " + line); cli@37: } chris@3: cli@37: this.out.write("DATA".getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("354 ")) { cli@37: throw new IOException("Unexpected reply: " + line); cli@37: } chris@3: cli@37: ArticleInputStream artStream = new ArticleInputStream(article); cli@37: for (int b = artStream.read(); b >= 0; b = artStream.read()) { cli@37: this.out.write(b); cli@37: } chris@3: cli@37: // Flush the binary stream; important because otherwise the output cli@37: // will be mixed with the PrintWriter. cli@37: this.out.flush(); cli@37: this.out.write("\r\n.\r\n".getBytes("UTF-8")); cli@37: this.out.flush(); cli@37: line = this.in.readLine(); cli@37: if (line == null || !line.startsWith("250 ")) { cli@37: throw new IOException("Unexpected reply: " + line); cli@37: } cli@37: } chris@3: }