Work on SMTPTransport.
authorcli
Wed, 14 Sep 2011 10:47:39 +0200
changeset 5968a6825a4f4d
parent 58 b2df305a13ce
child 60 39f1fadf50a0
Work on SMTPTransport.
src/org/sonews/mlgw/SMTPTransport.java
     1.1 --- a/src/org/sonews/mlgw/SMTPTransport.java	Tue Sep 13 23:34:16 2011 +0200
     1.2 +++ b/src/org/sonews/mlgw/SMTPTransport.java	Wed Sep 14 10:47:39 2011 +0200
     1.3 @@ -23,6 +23,8 @@
     1.4  import java.io.InputStreamReader;
     1.5  import java.net.Socket;
     1.6  import java.net.UnknownHostException;
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.List;
     1.9  import org.sonews.config.Config;
    1.10  import org.sonews.storage.Article;
    1.11  import org.sonews.util.io.ArticleInputStream;
    1.12 @@ -73,20 +75,8 @@
    1.13  		this.out.write(strBuf.toString().getBytes("UTF-8"));
    1.14  		this.out.flush();
    1.15  
    1.16 -		// Read reply: "250-example.org Hello example.net"
    1.17 -		String line = this.in.readLine();
    1.18 -		if (line == null || !line.startsWith("250")) {
    1.19 -			throw new IOException("Unexpected reply: " + line);
    1.20 -		}
    1.21 +		List<String> ehloReplies = readReply("250");
    1.22  
    1.23 -		// FIXME: More 250- lines possible!
    1.24 -
    1.25 -		// Read reply: "250 AUTH CRAM-MD5 LOGIN PLAIN"
    1.26 -		line = this.in.readLine();
    1.27 -		if (line == null || !line.startsWith("250 ")) {
    1.28 -			throw new IOException("Unexpected reply: " + line);
    1.29 -		}
    1.30 -		String[] authMethods = line.split(" ");
    1.31  		// TODO: Check for supported methods
    1.32  
    1.33  		// Do a PLAIN login
    1.34 @@ -98,8 +88,13 @@
    1.35  		this.out.write(strBuf.toString().getBytes("UTF-8"));
    1.36  		this.out.flush();
    1.37  
    1.38 +		readReply("334");
    1.39 +
    1.40 +		// Send PLAIN credentials to server
    1.41 +		
    1.42 +
    1.43  		// Read reply
    1.44 -		line = this.in.readLine();
    1.45 +		String line = this.in.readLine();
    1.46  		if (line == null || !line.startsWith("250 ")) {
    1.47  			throw new IOException("Unexpected reply: " + line);
    1.48  		}
    1.49 @@ -116,10 +111,7 @@
    1.50  		this.out.flush();
    1.51  
    1.52  		// Read reply
    1.53 -		String line = this.in.readLine();
    1.54 -		if (line == null || !line.startsWith("250 ")) {
    1.55 -			throw new IOException("Unexpected reply: " + line);
    1.56 -		}
    1.57 +		readReply("250");
    1.58  	}
    1.59  
    1.60  	public void login() throws IOException {
    1.61 @@ -132,6 +124,32 @@
    1.62  		}
    1.63  	}
    1.64  
    1.65 +	/**
    1.66 +	 * Read one or more exspected reply lines.
    1.67 +	 * @param expectedReply
    1.68 +	 * @return
    1.69 +	 * @throws IOException If the reply of the server does not fit the exspected
    1.70 +	 * reply code.
    1.71 +	 */
    1.72 +	private List<String> readReply(String expectedReply) throws IOException {
    1.73 +		List<String> replyStrings = new ArrayList<String>();
    1.74 +
    1.75 +		for(;;) {
    1.76 +			String line = this.in.readLine();
    1.77 +			if (line == null || !line.startsWith(expectedReply)) {
    1.78 +				throw new IOException("Unexpected reply: " + line);
    1.79 +			}
    1.80 +
    1.81 +			replyStrings.add(line);
    1.82 +
    1.83 +			if(line.charAt(3) == ' ') { // Last reply line
    1.84 +				break;
    1.85 +			}
    1.86 +		}
    1.87 +
    1.88 +		return replyStrings;
    1.89 +	}
    1.90 +
    1.91  	public void send(Article article, String mailFrom, String rcptTo)
    1.92  			throws IOException {
    1.93  		assert (article != null);