3 * see AUTHORS for the list of contributors
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.
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.
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/>.
18 package org.sonews.mlgw;
20 import java.io.BufferedOutputStream;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.net.Socket;
25 import java.net.UnknownHostException;
26 import org.sonews.config.Config;
27 import org.sonews.storage.Article;
28 import org.sonews.util.io.ArticleInputStream;
31 * Connects to a SMTP server and sends a given Article to it.
32 * @author Christian Lins
37 public static final String NEWLINE = "\r\n";
39 protected BufferedReader in;
40 protected BufferedOutputStream out;
41 protected Socket socket;
43 public SMTPTransport(String host, int port)
44 throws IOException, UnknownHostException {
45 this.socket = new Socket(host, port);
46 this.in = new BufferedReader(
47 new InputStreamReader(socket.getInputStream()));
48 this.out = new BufferedOutputStream(socket.getOutputStream());
50 // Read HELO from server
51 String line = this.in.readLine();
52 if (line == null || !line.startsWith("220 ")) {
53 throw new IOException("Invalid HELO from server: " + line);
59 this.out.write("QUIT".getBytes("UTF-8"));
66 private void ehlo(String hostname) throws IOException {
67 StringBuilder strBuf = new StringBuilder();
68 strBuf.append("EHLO ");
69 strBuf.append(hostname);
70 strBuf.append(NEWLINE);
72 // Send EHLO to server
73 this.out.write(strBuf.toString().getBytes("UTF-8"));
76 // Read reply: "250-example.org Hello example.net"
77 String line = this.in.readLine();
78 if (line == null || !line.startsWith("250")) {
79 throw new IOException("Unexpected reply: " + line);
82 // FIXME: More 250- lines possible!
84 // Read reply: "250 AUTH CRAM-MD5 LOGIN PLAIN"
85 line = this.in.readLine();
86 if (line == null || !line.startsWith("250 ")) {
87 throw new IOException("Unexpected reply: " + line);
89 String[] authMethods = line.split(" ");
90 // TODO: Check for supported methods
93 strBuf = new StringBuilder();
94 strBuf.append("AUTH PLAIN");
95 strBuf.append(NEWLINE);
97 // Send AUTH to server
98 this.out.write(strBuf.toString().getBytes("UTF-8"));
102 line = this.in.readLine();
103 if (line == null || !line.startsWith("250 ")) {
104 throw new IOException("Unexpected reply: " + line);
108 private void helo(String hostname) throws IOException {
109 StringBuilder heloStr = new StringBuilder();
110 heloStr.append("HELO ");
111 heloStr.append(hostname);
112 heloStr.append(NEWLINE);
114 // Send HELO to server
115 this.out.write(heloStr.toString().getBytes("UTF-8"));
119 String line = this.in.readLine();
120 if (line == null || !line.startsWith("250 ")) {
121 throw new IOException("Unexpected reply: " + line);
125 public void login() throws IOException {
126 String hostname = Config.inst().get(Config.HOSTNAME, "localhost");
127 String auth = Config.inst().get(Config.MLSEND_AUTH, "none");
128 if(auth.equals("none")) {
135 public void send(Article article, String mailFrom, String rcptTo)
137 assert (article != null);
138 assert (mailFrom != null);
139 assert (rcptTo != null);
141 this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
143 String line = this.in.readLine();
144 if (line == null || !line.startsWith("250 ")) {
145 throw new IOException("Unexpected reply: " + line);
148 this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
150 line = this.in.readLine();
151 if (line == null || !line.startsWith("250 ")) {
152 throw new IOException("Unexpected reply: " + line);
155 this.out.write("DATA".getBytes("UTF-8"));
157 line = this.in.readLine();
158 if (line == null || !line.startsWith("354 ")) {
159 throw new IOException("Unexpected reply: " + line);
162 ArticleInputStream artStream = new ArticleInputStream(article);
163 for (int b = artStream.read(); b >= 0; b = artStream.read()) {
167 // Flush the binary stream; important because otherwise the output
168 // will be mixed with the PrintWriter.
170 this.out.write("\r\n.\r\n".getBytes("UTF-8"));
172 line = this.in.readLine();
173 if (line == null || !line.startsWith("250 ")) {
174 throw new IOException("Unexpected reply: " + line);