src/org/sonews/mlgw/SMTPTransport.java
author cli
Sun Aug 29 18:17:37 2010 +0200 (2010-08-29)
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 58 b2df305a13ce
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.mlgw;
    20 
    21 import java.io.BufferedOutputStream;
    22 import java.io.BufferedReader;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.net.Socket;
    26 import java.net.UnknownHostException;
    27 import org.sonews.config.Config;
    28 import org.sonews.storage.Article;
    29 import org.sonews.util.io.ArticleInputStream;
    30 
    31 /**
    32  * Connects to a SMTP server and sends a given Article to it.
    33  * @author Christian Lins
    34  * @since sonews/1.0
    35  */
    36 class SMTPTransport
    37 {
    38 
    39 	protected BufferedReader in;
    40 	protected BufferedOutputStream out;
    41 	protected Socket socket;
    42 
    43 	public SMTPTransport(String host, int port)
    44 		throws IOException, UnknownHostException
    45 	{
    46 		socket = new Socket(host, port);
    47 		this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    48 		this.out = new BufferedOutputStream(socket.getOutputStream());
    49 
    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);
    54 		}
    55 
    56 		// Send HELO to server
    57 		this.out.write(
    58 			("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
    59 		this.out.flush();
    60 		line = this.in.readLine();
    61 		if (line == null || !line.startsWith("250 ")) {
    62 			throw new IOException("Unexpected reply: " + line);
    63 		}
    64 	}
    65 
    66 	public SMTPTransport(String host)
    67 		throws IOException
    68 	{
    69 		this(host, 25);
    70 	}
    71 
    72 	public void close()
    73 		throws IOException
    74 	{
    75 		this.out.write("QUIT".getBytes("UTF-8"));
    76 		this.out.flush();
    77 		this.in.readLine();
    78 
    79 		this.socket.close();
    80 	}
    81 
    82 	public void send(Article article, String mailFrom, String rcptTo)
    83 		throws IOException
    84 	{
    85 		assert (article != null);
    86 		assert (mailFrom != null);
    87 		assert (rcptTo != null);
    88 
    89 		this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
    90 		this.out.flush();
    91 		String line = this.in.readLine();
    92 		if (line == null || !line.startsWith("250 ")) {
    93 			throw new IOException("Unexpected reply: " + line);
    94 		}
    95 
    96 		this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
    97 		this.out.flush();
    98 		line = this.in.readLine();
    99 		if (line == null || !line.startsWith("250 ")) {
   100 			throw new IOException("Unexpected reply: " + line);
   101 		}
   102 
   103 		this.out.write("DATA".getBytes("UTF-8"));
   104 		this.out.flush();
   105 		line = this.in.readLine();
   106 		if (line == null || !line.startsWith("354 ")) {
   107 			throw new IOException("Unexpected reply: " + line);
   108 		}
   109 
   110 		ArticleInputStream artStream = new ArticleInputStream(article);
   111 		for (int b = artStream.read(); b >= 0; b = artStream.read()) {
   112 			this.out.write(b);
   113 		}
   114 
   115 		// Flush the binary stream; important because otherwise the output
   116 		// will be mixed with the PrintWriter.
   117 		this.out.flush();
   118 		this.out.write("\r\n.\r\n".getBytes("UTF-8"));
   119 		this.out.flush();
   120 		line = this.in.readLine();
   121 		if (line == null || !line.startsWith("250 ")) {
   122 			throw new IOException("Unexpected reply: " + line);
   123 		}
   124 	}
   125 }