annotate src/goodjava/mail/Smtp.java @ 1610:94ea190714dd

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 13 May 2021 17:26:34 -0600
parents d3728e3e5af3
children e34b73678a4f
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;
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
9 import java.util.Map;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
10 import goodjava.util.GoodUtils;
1582
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public class Smtp {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 private final char[] buf = new char[1000];
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 private final Socket socket;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private final Reader reader;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 private final Writer writer;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public final String ehlo;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
20 public Smtp(Socket socket) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this.socket = socket;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 this.reader = new InputStreamReader(socket.getInputStream());
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.writer = new OutputStreamWriter(socket.getOutputStream());
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 if( !s.startsWith("220") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
26 throw new MailException(s);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 write( "EHLO\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 ehlo = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if( !ehlo.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
30 throw new MailException(ehlo);
1582
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
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
33 public String authenticate(String username,String password) throws IOException, MailException {
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
34 String s = "\0" + username + "\0" + password;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
35 s = GoodUtils.base64Encode(s);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
36 write( "AUTH PLAIN " + s + "\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
37 String r = read();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
38 if( !r.startsWith("235") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
39 throw new MailException(r);
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
40 return r;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
41 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
42
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
43 public void send(Message msg) throws IOException, MailException {
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
44 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
45 String name = entry.getKey();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
46 String value = entry.getValue();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
47 if( name.equalsIgnoreCase("from") ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
48 from(value);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
49 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
50 if( name.equalsIgnoreCase("to")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
51 || name.equalsIgnoreCase("cc")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
52 || name.equalsIgnoreCase("bcc")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
53 ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
54 for( String s : value.split(",") ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
55 to(s);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
56 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
57 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
58 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
59 data( msg.toText() );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
60 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
61
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
62 public void close() throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 write( "QUIT\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 if( !s.startsWith("221") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
66 throw new MailException(s);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 socket.close();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
70 public String from(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 write( "MAIL FROM: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
74 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
78 public String to(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 write( "RCPT TO: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
82 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 return r;
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
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
86 public String data(String text) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 if( !text.endsWith("\r\n") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
88 throw new MailException("text must end with \\r\\n");
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 text = text.replace("\r\n.","\r\n..");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 write( "DATA\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 if( !r.startsWith("354") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
93 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 write( text + ".\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
97 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 private String read() throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 int n = reader.read(buf);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 return new String(buf,0,n);
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 private void write(String s) throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 writer.write(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 writer.flush();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 }