Work on SMTPTransport.
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 java.util.ArrayList;
27 import java.util.List;
28 import org.sonews.config.Config;
29 import org.sonews.storage.Article;
30 import org.sonews.util.io.ArticleInputStream;
33 * Connects to a SMTP server and sends a given Article to it.
34 * @author Christian Lins
39 public static final String NEWLINE = "\r\n";
41 protected BufferedReader in;
42 protected BufferedOutputStream out;
43 protected Socket socket;
45 public SMTPTransport(String host, int port)
46 throws IOException, UnknownHostException {
47 this.socket = new Socket(host, port);
48 this.in = new BufferedReader(
49 new InputStreamReader(socket.getInputStream()));
50 this.out = new BufferedOutputStream(socket.getOutputStream());
52 // Read HELO from server
53 String line = this.in.readLine();
54 if (line == null || !line.startsWith("220 ")) {
55 throw new IOException("Invalid HELO from server: " + line);
61 this.out.write("QUIT".getBytes("UTF-8"));
68 private void ehlo(String hostname) throws IOException {
69 StringBuilder strBuf = new StringBuilder();
70 strBuf.append("EHLO ");
71 strBuf.append(hostname);
72 strBuf.append(NEWLINE);
74 // Send EHLO to server
75 this.out.write(strBuf.toString().getBytes("UTF-8"));
78 List<String> ehloReplies = readReply("250");
80 // TODO: Check for supported methods
83 strBuf = new StringBuilder();
84 strBuf.append("AUTH PLAIN");
85 strBuf.append(NEWLINE);
87 // Send AUTH to server
88 this.out.write(strBuf.toString().getBytes("UTF-8"));
93 // Send PLAIN credentials to server
97 String line = this.in.readLine();
98 if (line == null || !line.startsWith("250 ")) {
99 throw new IOException("Unexpected reply: " + line);
103 private void helo(String hostname) throws IOException {
104 StringBuilder heloStr = new StringBuilder();
105 heloStr.append("HELO ");
106 heloStr.append(hostname);
107 heloStr.append(NEWLINE);
109 // Send HELO to server
110 this.out.write(heloStr.toString().getBytes("UTF-8"));
117 public void login() throws IOException {
118 String hostname = Config.inst().get(Config.HOSTNAME, "localhost");
119 String auth = Config.inst().get(Config.MLSEND_AUTH, "none");
120 if(auth.equals("none")) {
128 * Read one or more exspected reply lines.
129 * @param expectedReply
131 * @throws IOException If the reply of the server does not fit the exspected
134 private List<String> readReply(String expectedReply) throws IOException {
135 List<String> replyStrings = new ArrayList<String>();
138 String line = this.in.readLine();
139 if (line == null || !line.startsWith(expectedReply)) {
140 throw new IOException("Unexpected reply: " + line);
143 replyStrings.add(line);
145 if(line.charAt(3) == ' ') { // Last reply line
153 public void send(Article article, String mailFrom, String rcptTo)
155 assert (article != null);
156 assert (mailFrom != null);
157 assert (rcptTo != null);
159 this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
161 String line = this.in.readLine();
162 if (line == null || !line.startsWith("250 ")) {
163 throw new IOException("Unexpected reply: " + line);
166 this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
168 line = this.in.readLine();
169 if (line == null || !line.startsWith("250 ")) {
170 throw new IOException("Unexpected reply: " + line);
173 this.out.write("DATA".getBytes("UTF-8"));
175 line = this.in.readLine();
176 if (line == null || !line.startsWith("354 ")) {
177 throw new IOException("Unexpected reply: " + line);
180 ArticleInputStream artStream = new ArticleInputStream(article);
181 for (int b = artStream.read(); b >= 0; b = artStream.read()) {
185 // Flush the binary stream; important because otherwise the output
186 // will be mixed with the PrintWriter.
188 this.out.write("\r\n.\r\n".getBytes("UTF-8"));
190 line = this.in.readLine();
191 if (line == null || !line.startsWith("250 ")) {
192 throw new IOException("Unexpected reply: " + line);