annotate src/goodjava/mail/Smtp.java @ 1769:9d9683e76496

bug fix
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Jun 2023 19:17:26 -0600
parents 1ffe1e06ea55
children 0eb615de1f80
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);
1765
1ffe1e06ea55 SSL for port 465
Franklin Schmidt <fschmidt@gmail.com>
parents: 1686
diff changeset
27 write( "EHLO whatever\r\n" );
1582
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 }
1686
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
50 }
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
51 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) {
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
52 String name = entry.getKey();
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
53 String value = entry.getValue();
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
54 if( name.equalsIgnoreCase("to")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
55 || name.equalsIgnoreCase("cc")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
56 || name.equalsIgnoreCase("bcc")
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 for( String s : value.split(",") ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
59 to(s);
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 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
62 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
63 data( msg.toText() );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
64 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
65
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
66 public void close() throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 write( "QUIT\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 if( !s.startsWith("221") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
70 throw new MailException(s);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 socket.close();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
74 public String from(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 write( "MAIL FROM: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
78 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return r;
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
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
82 public String to(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 write( "RCPT TO: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
86 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
90 public String data(String text) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 if( !text.endsWith("\r\n") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
92 throw new MailException("text must end with \\r\\n");
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 text = text.replace("\r\n.","\r\n..");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 write( "DATA\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 if( !r.startsWith("354") )
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 write( text + ".\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
101 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 }
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 private String read() throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 int n = reader.read(buf);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 return new String(buf,0,n);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 }
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 private void write(String s) throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 writer.write(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 writer.flush();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 }