annotate src/goodjava/mail/Smtp.java @ 1583:1cc6c7fa803d

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Mar 2021 02:22:09 -0700
parents f28cc30d56cb
children d3728e3e5af3
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public Smtp(Socket socket) throws IOException, SmtpException {
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 throw new SmtpException(s);
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 throw new SmtpException(ehlo);
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
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
33 public String authenticate(String username,String password) throws IOException, SmtpException {
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") )
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
39 throw new SmtpException(r);
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
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
43 public void send(Message msg) throws IOException, SmtpException {
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
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 public void close() throws IOException, SmtpException {
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 throw new SmtpException(s);
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 public String from(String address) throws IOException, SmtpException {
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 throw new SmtpException(r);
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 public String to(String address) throws IOException, SmtpException {
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 throw new SmtpException(r);
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 public String data(String text) throws IOException, SmtpException {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 if( !text.endsWith("\r\n") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 throw new SmtpException("text must end with \\r\\n");
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 throw new SmtpException(r);
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") )
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 throw new SmtpException(r);
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
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 public static void main(String[] args) throws Exception {
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 Socket socket = new Socket("smtpcorp.com",2525);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 Smtp smtp = new Smtp(socket);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 smtp.authenticate("smtp@luan.software","luanhost");
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 smtp.from("smtp@luan.software");
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
116 smtp.to(" fschmidt@gmail.com");
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 String text = "\r\n"
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
118 +"test3\r\n"
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 +".q\r\n"
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 +"x\r\n"
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1582
diff changeset
121 +"rg; ;lrg dsl rgj errlgerrg neskrjg skrg rdsg drskrg sd;gr s;kgr skrg skrg sdg ds fg;ks gegr erg ;sg sd; g;sdr gsklrg sg s;kkrg s;hg ;slrg ;elrg ;reg r;g ;r g;er g;ler g;e g; g;r g rg; srkd fjl kj kklsjrg lsk gskdf;rs gkrj glj grekjs lksjgkjn kjslg rklrg ;rsd; kj drsg akrglk kalrgklrsdnrgkgj;r ;s ns b;n;sn ;njslk r;n\r\n"
1582
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 ;
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 smtp.data(text);
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 smtp.close();
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 }
f28cc30d56cb start goodjava/mail
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 }