annotate src/goodjava/mail/Smtp.java @ 1582:f28cc30d56cb

start goodjava/mail
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 06 Mar 2021 21:13:34 -0700
parents
children 1cc6c7fa803d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.mail;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Reader;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.InputStreamReader;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.Writer;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.OutputStreamWriter;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.io.IOException;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.net.Socket;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.Base64;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public class Smtp {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 private final char[] buf = new char[1000];
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 private final Socket socket;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 private final Reader reader;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private final Writer writer;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 public final String ehlo;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public Smtp(Socket socket) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 this.socket = socket;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this.reader = new InputStreamReader(socket.getInputStream());
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 this.writer = new OutputStreamWriter(socket.getOutputStream());
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 if( !s.startsWith("220") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 throw new SmtpException(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 write( "EHLO\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 ehlo = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if( !ehlo.startsWith("250") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 throw new SmtpException(ehlo);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 public void close() throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 write( "QUIT\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 if( !s.startsWith("221") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 throw new SmtpException(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 socket.close();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 public String authenticate(String username,String password) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 String s = "\0" + username + "\0" + password;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 s = Base64.getEncoder().encodeToString(s.getBytes());
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 write( "AUTH PLAIN " + s + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 if( !r.startsWith("235") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 throw new SmtpException(r);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 public String from(String address) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 write( "MAIL FROM: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if( !r.startsWith("250") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 throw new SmtpException(r);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 public String to(String address) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 write( "RCPT TO: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 if( !r.startsWith("250") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 throw new SmtpException(r);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 public String data(String text) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 if( !text.endsWith("\r\n") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 throw new SmtpException("text must end with \\r\\n");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 text = text.replace("\r\n.","\r\n..");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 write( "DATA\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 if( !r.startsWith("354") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 throw new SmtpException(r);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 write( text + ".\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 if( !r.startsWith("250") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 throw new SmtpException(r);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 private String read() throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 int n = reader.read(buf);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 return new String(buf,0,n);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 private void write(String s) throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 writer.write(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 writer.flush();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 public static void main(String[] args) throws Exception {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 Socket socket = new Socket("smtpcorp.com",2525);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 Smtp smtp = new Smtp(socket);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 smtp.authenticate("smtp@luan.software","luanhost");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 smtp.from("smtp@luan.software");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 smtp.to("fschmidt@gmail.com");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 String text = "\r\n"
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 +"test2\r\n"
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 +".q\r\n"
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 +"x\r\n"
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 ;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 smtp.data(text);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 smtp.close();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 }