changeset 1582:f28cc30d56cb

start goodjava/mail
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 06 Mar 2021 21:13:34 -0700
parents dbf63bef4b86
children 1cc6c7fa803d
files src/goodjava/mail/Smtp.java src/goodjava/mail/SmtpException.java
diffstat 2 files changed, 114 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
diff -r dbf63bef4b86 -r f28cc30d56cb src/goodjava/mail/Smtp.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/mail/Smtp.java	Sat Mar 06 21:13:34 2021 -0700
@@ -0,0 +1,105 @@
+package goodjava.mail;
+
+import java.io.Reader;
+import java.io.InputStreamReader;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+import java.io.IOException;
+import java.net.Socket;
+import java.util.Base64;
+
+
+public class Smtp {
+	private final char[] buf = new char[1000];
+	private final Socket socket;
+	private final Reader reader;
+	private final Writer writer;
+	public final String ehlo;
+
+	public Smtp(Socket socket) throws IOException, SmtpException {
+		this.socket = socket;
+		this.reader = new InputStreamReader(socket.getInputStream());
+		this.writer = new OutputStreamWriter(socket.getOutputStream());
+		String s = read();
+		if( !s.startsWith("220") )
+			throw new SmtpException(s);
+		write( "EHLO\r\n" );
+		ehlo = read();
+		if( !ehlo.startsWith("250") )
+			throw new SmtpException(ehlo);
+	}
+
+	public void close() throws IOException, SmtpException {
+		write( "QUIT\r\n" );
+		String s = read();
+		if( !s.startsWith("221") )
+			throw new SmtpException(s);
+		socket.close();
+	}
+
+	public String authenticate(String username,String password) throws IOException, SmtpException {
+		String s = "\0" + username + "\0" + password;
+		s = Base64.getEncoder().encodeToString(s.getBytes());
+		write( "AUTH PLAIN " + s + "\r\n" );
+		String r = read();
+		if( !r.startsWith("235") )
+			throw new SmtpException(r);
+		return r;
+	}
+
+	public String from(String address) throws IOException, SmtpException {
+		write( "MAIL FROM: " + address + "\r\n" );
+		String r = read();
+		if( !r.startsWith("250") )
+			throw new SmtpException(r);
+		return r;
+	}
+
+	public String to(String address) throws IOException, SmtpException {
+		write( "RCPT TO: " + address + "\r\n" );
+		String r = read();
+		if( !r.startsWith("250") )
+			throw new SmtpException(r);
+		return r;
+	}
+
+	public String data(String text) throws IOException, SmtpException {
+		if( !text.endsWith("\r\n") )
+			throw new SmtpException("text must end with \\r\\n");
+		text = text.replace("\r\n.","\r\n..");
+		write( "DATA\r\n" );
+		String r = read();
+		if( !r.startsWith("354") )
+			throw new SmtpException(r);
+		write( text + ".\r\n" );
+		r = read();
+		if( !r.startsWith("250") )
+			throw new SmtpException(r);
+		return r;
+	}
+
+	private String read() throws IOException {
+		int n = reader.read(buf);
+		return new String(buf,0,n);
+	}
+
+	private void write(String s) throws IOException {
+		writer.write(s);
+		writer.flush();
+	}
+
+	public static void main(String[] args) throws Exception {
+		Socket socket = new Socket("smtpcorp.com",2525);
+		Smtp smtp = new Smtp(socket);
+		smtp.authenticate("smtp@luan.software","luanhost");
+		smtp.from("smtp@luan.software");
+		smtp.to("fschmidt@gmail.com");
+		String text = "\r\n"
+			+"test2\r\n"
+			+".q\r\n"
+			+"x\r\n"
+		;
+		smtp.data(text);
+		smtp.close();
+	}
+}
diff -r dbf63bef4b86 -r f28cc30d56cb src/goodjava/mail/SmtpException.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/mail/SmtpException.java	Sat Mar 06 21:13:34 2021 -0700
@@ -0,0 +1,9 @@
+package goodjava.mail;
+
+
+public class SmtpException extends Exception {
+
+	public SmtpException(String msg) {
+		super(msg);
+	}
+}