1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/mlgw/SMTPTransport.java Sun Aug 29 17:28:58 2010 +0200
1.3 @@ -0,0 +1,133 @@
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.IOException;
1.27 +import java.io.InputStreamReader;
1.28 +import java.net.Socket;
1.29 +import java.net.UnknownHostException;
1.30 +import org.sonews.config.Config;
1.31 +import org.sonews.storage.Article;
1.32 +import org.sonews.util.io.ArticleInputStream;
1.33 +
1.34 +/**
1.35 + * Connects to a SMTP server and sends a given Article to it.
1.36 + * @author Christian Lins
1.37 + * @since sonews/1.0
1.38 + */
1.39 +class SMTPTransport
1.40 +{
1.41 +
1.42 + protected BufferedReader in;
1.43 + protected BufferedOutputStream out;
1.44 + protected Socket socket;
1.45 +
1.46 + public SMTPTransport(String host, int port)
1.47 + throws IOException, UnknownHostException
1.48 + {
1.49 + socket = new Socket(host, port);
1.50 + this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
1.51 + this.out = new BufferedOutputStream(socket.getOutputStream());
1.52 +
1.53 + // Read helo from server
1.54 + String line = this.in.readLine();
1.55 + if(line == null || !line.startsWith("220 "))
1.56 + {
1.57 + throw new IOException("Invalid helo from server: " + line);
1.58 + }
1.59 +
1.60 + // Send HELO to server
1.61 + this.out.write(
1.62 + ("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
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.write("QUIT".getBytes("UTF-8"));
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 + assert(article != null);
1.91 + assert(mailFrom != null);
1.92 + assert(rcptTo != null);
1.93 +
1.94 + this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
1.95 + this.out.flush();
1.96 + String line = this.in.readLine();
1.97 + if(line == null || !line.startsWith("250 "))
1.98 + {
1.99 + throw new IOException("Unexpected reply: " + line);
1.100 + }
1.101 +
1.102 + this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
1.103 + this.out.flush();
1.104 + line = this.in.readLine();
1.105 + if(line == null || !line.startsWith("250 "))
1.106 + {
1.107 + throw new IOException("Unexpected reply: " + line);
1.108 + }
1.109 +
1.110 + this.out.write("DATA".getBytes("UTF-8"));
1.111 + this.out.flush();
1.112 + line = this.in.readLine();
1.113 + if(line == null || !line.startsWith("354 "))
1.114 + {
1.115 + throw new IOException("Unexpected reply: " + line);
1.116 + }
1.117 +
1.118 + ArticleInputStream artStream = new ArticleInputStream(article);
1.119 + for(int b = artStream.read(); b >= 0; b = artStream.read())
1.120 + {
1.121 + this.out.write(b);
1.122 + }
1.123 +
1.124 + // Flush the binary stream; important because otherwise the output
1.125 + // will be mixed with the PrintWriter.
1.126 + this.out.flush();
1.127 + this.out.write("\r\n.\r\n".getBytes("UTF-8"));
1.128 + this.out.flush();
1.129 + line = this.in.readLine();
1.130 + if(line == null || !line.startsWith("250 "))
1.131 + {
1.132 + throw new IOException("Unexpected reply: " + line);
1.133 + }
1.134 + }
1.135 +
1.136 +}