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