src/org/sonews/mlgw/SMTPTransport.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 58 b2df305a13ce
     1.1 --- a/src/org/sonews/mlgw/SMTPTransport.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/mlgw/SMTPTransport.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -36,98 +36,90 @@
     1.4  class SMTPTransport
     1.5  {
     1.6  
     1.7 -  protected BufferedReader       in;
     1.8 -  protected BufferedOutputStream out;
     1.9 -  protected Socket               socket;
    1.10 +	protected BufferedReader in;
    1.11 +	protected BufferedOutputStream out;
    1.12 +	protected Socket socket;
    1.13  
    1.14 -  public SMTPTransport(String host, int port)
    1.15 -    throws IOException, UnknownHostException
    1.16 -  {
    1.17 -    socket = new Socket(host, port);
    1.18 -    this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.19 -    this.out = new BufferedOutputStream(socket.getOutputStream());
    1.20 +	public SMTPTransport(String host, int port)
    1.21 +		throws IOException, UnknownHostException
    1.22 +	{
    1.23 +		socket = new Socket(host, port);
    1.24 +		this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.25 +		this.out = new BufferedOutputStream(socket.getOutputStream());
    1.26  
    1.27 -    // Read helo from server
    1.28 -    String line = this.in.readLine();
    1.29 -    if(line == null || !line.startsWith("220 "))
    1.30 -    {
    1.31 -      throw new IOException("Invalid helo from server: " + line);
    1.32 -    }
    1.33 +		// Read helo from server
    1.34 +		String line = this.in.readLine();
    1.35 +		if (line == null || !line.startsWith("220 ")) {
    1.36 +			throw new IOException("Invalid helo from server: " + line);
    1.37 +		}
    1.38  
    1.39 -    // Send HELO to server
    1.40 -    this.out.write(
    1.41 -      ("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
    1.42 -    this.out.flush();
    1.43 -    line = this.in.readLine();
    1.44 -    if(line == null || !line.startsWith("250 "))
    1.45 -    {
    1.46 -      throw new IOException("Unexpected reply: " + line);
    1.47 -    }
    1.48 -  }
    1.49 +		// Send HELO to server
    1.50 +		this.out.write(
    1.51 +			("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
    1.52 +		this.out.flush();
    1.53 +		line = this.in.readLine();
    1.54 +		if (line == null || !line.startsWith("250 ")) {
    1.55 +			throw new IOException("Unexpected reply: " + line);
    1.56 +		}
    1.57 +	}
    1.58  
    1.59 -  public SMTPTransport(String host)
    1.60 -    throws IOException
    1.61 -  {
    1.62 -    this(host, 25);
    1.63 -  }
    1.64 +	public SMTPTransport(String host)
    1.65 +		throws IOException
    1.66 +	{
    1.67 +		this(host, 25);
    1.68 +	}
    1.69  
    1.70 -  public void close()
    1.71 -    throws IOException
    1.72 -  {
    1.73 -    this.out.write("QUIT".getBytes("UTF-8"));
    1.74 -    this.out.flush();
    1.75 -    this.in.readLine();
    1.76 +	public void close()
    1.77 +		throws IOException
    1.78 +	{
    1.79 +		this.out.write("QUIT".getBytes("UTF-8"));
    1.80 +		this.out.flush();
    1.81 +		this.in.readLine();
    1.82  
    1.83 -    this.socket.close();
    1.84 -  }
    1.85 +		this.socket.close();
    1.86 +	}
    1.87  
    1.88 -  public void send(Article article, String mailFrom, String rcptTo)
    1.89 -    throws IOException
    1.90 -  {
    1.91 -    assert(article != null);
    1.92 -    assert(mailFrom != null);
    1.93 -    assert(rcptTo != null);
    1.94 +	public void send(Article article, String mailFrom, String rcptTo)
    1.95 +		throws IOException
    1.96 +	{
    1.97 +		assert (article != null);
    1.98 +		assert (mailFrom != null);
    1.99 +		assert (rcptTo != null);
   1.100  
   1.101 -    this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
   1.102 -    this.out.flush();
   1.103 -    String line = this.in.readLine();
   1.104 -    if(line == null || !line.startsWith("250 "))
   1.105 -    {
   1.106 -      throw new IOException("Unexpected reply: " + line);
   1.107 -    }
   1.108 +		this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
   1.109 +		this.out.flush();
   1.110 +		String line = this.in.readLine();
   1.111 +		if (line == null || !line.startsWith("250 ")) {
   1.112 +			throw new IOException("Unexpected reply: " + line);
   1.113 +		}
   1.114  
   1.115 -    this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
   1.116 -    this.out.flush();
   1.117 -    line  = this.in.readLine();
   1.118 -    if(line == null || !line.startsWith("250 "))
   1.119 -    {
   1.120 -      throw new IOException("Unexpected reply: " + line);
   1.121 -    }
   1.122 +		this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
   1.123 +		this.out.flush();
   1.124 +		line = this.in.readLine();
   1.125 +		if (line == null || !line.startsWith("250 ")) {
   1.126 +			throw new IOException("Unexpected reply: " + line);
   1.127 +		}
   1.128  
   1.129 -    this.out.write("DATA".getBytes("UTF-8"));
   1.130 -    this.out.flush();
   1.131 -    line = this.in.readLine();
   1.132 -    if(line == null || !line.startsWith("354 "))
   1.133 -    {
   1.134 -      throw new IOException("Unexpected reply: " + line);
   1.135 -    }
   1.136 +		this.out.write("DATA".getBytes("UTF-8"));
   1.137 +		this.out.flush();
   1.138 +		line = this.in.readLine();
   1.139 +		if (line == null || !line.startsWith("354 ")) {
   1.140 +			throw new IOException("Unexpected reply: " + line);
   1.141 +		}
   1.142  
   1.143 -    ArticleInputStream   artStream = new ArticleInputStream(article);
   1.144 -    for(int b = artStream.read(); b >= 0; b = artStream.read())
   1.145 -    {
   1.146 -      this.out.write(b);
   1.147 -    }
   1.148 +		ArticleInputStream artStream = new ArticleInputStream(article);
   1.149 +		for (int b = artStream.read(); b >= 0; b = artStream.read()) {
   1.150 +			this.out.write(b);
   1.151 +		}
   1.152  
   1.153 -    // Flush the binary stream; important because otherwise the output
   1.154 -    // will be mixed with the PrintWriter.
   1.155 -    this.out.flush();
   1.156 -    this.out.write("\r\n.\r\n".getBytes("UTF-8"));
   1.157 -    this.out.flush();
   1.158 -    line = this.in.readLine();
   1.159 -    if(line == null || !line.startsWith("250 "))
   1.160 -    {
   1.161 -      throw new IOException("Unexpected reply: " + line);
   1.162 -    }
   1.163 -  }
   1.164 -
   1.165 +		// Flush the binary stream; important because otherwise the output
   1.166 +		// will be mixed with the PrintWriter.
   1.167 +		this.out.flush();
   1.168 +		this.out.write("\r\n.\r\n".getBytes("UTF-8"));
   1.169 +		this.out.flush();
   1.170 +		line = this.in.readLine();
   1.171 +		if (line == null || !line.startsWith("250 ")) {
   1.172 +			throw new IOException("Unexpected reply: " + line);
   1.173 +		}
   1.174 +	}
   1.175  }