annotate src/goodjava/mail/Smtp.java @ 1831:8f9ae295bf6a default tip

add Hosted.authorize
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 26 Sep 2024 15:07:45 -0600
parents d72a52232f79
children
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
1829
0eb615de1f80 add mail helo
Franklin Schmidt <fschmidt@gmail.com>
parents: 1765
diff changeset
20 public Smtp(Socket socket,String helo) 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);
1829
0eb615de1f80 add mail helo
Franklin Schmidt <fschmidt@gmail.com>
parents: 1765
diff changeset
27 write( "EHLO " + helo + "\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
1830
d72a52232f79 mail - minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1829
diff changeset
33 public Smtp(Socket socket) throws IOException, MailException {
d72a52232f79 mail - minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1829
diff changeset
34 this(socket,"GoodJava");
d72a52232f79 mail - minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1829
diff changeset
35 }
d72a52232f79 mail - minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1829
diff changeset
36
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
37 public String authenticate(String username,String password) throws IOException, MailException {
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
38 String s = "\0" + username + "\0" + password;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
39 s = GoodUtils.base64Encode(s);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
40 write( "AUTH PLAIN " + s + "\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
41 String r = read();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
42 if( !r.startsWith("235") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
43 throw new MailException(r);
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
44 return r;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
45 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
46
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
47 public void send(Message msg) throws IOException, MailException {
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
48 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
49 String name = entry.getKey();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
50 String value = entry.getValue();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
51 if( name.equalsIgnoreCase("from") ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
52 from(value);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
53 }
1686
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
54 }
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
55 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) {
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
56 String name = entry.getKey();
e34b73678a4f mail fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
57 String value = entry.getValue();
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
58 if( name.equalsIgnoreCase("to")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
59 || name.equalsIgnoreCase("cc")
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
60 || name.equalsIgnoreCase("bcc")
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 for( String s : value.split(",") ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
63 to(s);
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 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
66 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
67 data( msg.toText() );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
68 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
69
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
70 public void close() throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 write( "QUIT\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 String s = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if( !s.startsWith("221") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
74 throw new MailException(s);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 socket.close();
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 from(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 write( "MAIL FROM: " + 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 to(String address) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 write( "RCPT TO: " + address + "\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
90 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
94 public String data(String text) throws IOException, MailException {
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 if( !text.endsWith("\r\n") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
96 throw new MailException("text must end with \\r\\n");
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 text = text.replace("\r\n.","\r\n..");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 write( "DATA\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 String r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 if( !r.startsWith("354") )
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 write( text + ".\r\n" );
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 r = read();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 if( !r.startsWith("250") )
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
105 throw new MailException(r);
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return r;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 }
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 private String read() throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 int n = reader.read(buf);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return new String(buf,0,n);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
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 private void write(String s) throws IOException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 writer.write(s);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 writer.flush();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 }