# HG changeset patch # User cli # Date 1316032762 -7200 # Node ID da0c30d28e22dd0d10412822defd6eac95e54004 # Parent 39f1fadf50a03c61c1d83417e36e2b3b59882f90 SMTPSender should now support PLAIN SMTP Authentication (issue #8) diff -r 39f1fadf50a0 -r da0c30d28e22 src/org/sonews/mlgw/SMTPTransport.java --- a/src/org/sonews/mlgw/SMTPTransport.java Wed Sep 14 12:54:40 2011 +0200 +++ b/src/org/sonews/mlgw/SMTPTransport.java Wed Sep 14 22:39:22 2011 +0200 @@ -21,10 +21,12 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.codec.binary.Base64; import org.sonews.config.Config; import org.sonews.storage.Article; import org.sonews.util.io.ArticleInputStream; @@ -65,6 +67,16 @@ this.socket.close(); } + private byte[] createCredentials() throws UnsupportedEncodingException { + String user = Config.inst().get(Config.MLSEND_USER, ""); + String pass = Config.inst().get(Config.MLSEND_PASSWORD, ""); + StringBuilder credBuf = new StringBuilder(); + credBuf.append(user); + credBuf.append("\u0000"); + credBuf.append(pass); + return Base64.encodeBase64(credBuf.toString().getBytes("UTF-8")); + } + private void ehlo(String hostname) throws IOException { StringBuilder strBuf = new StringBuilder(); strBuf.append("EHLO "); @@ -91,13 +103,11 @@ readReply("334"); // Send PLAIN credentials to server - + this.out.write(createCredentials()); + this.out.flush(); - // Read reply - String line = this.in.readLine(); - if (line == null || !line.startsWith("250 ")) { - throw new IOException("Unexpected reply: " + line); - } + // Read reply of successful login + readReply("235"); } private void helo(String hostname) throws IOException {