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 }
|
1686
|
50 }
|
|
51 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) {
|
|
52 String name = entry.getKey();
|
|
53 String value = entry.getValue();
|
1583
|
54 if( name.equalsIgnoreCase("to")
|
|
55 || name.equalsIgnoreCase("cc")
|
|
56 || name.equalsIgnoreCase("bcc")
|
|
57 ) {
|
|
58 for( String s : value.split(",") ) {
|
|
59 to(s);
|
|
60 }
|
|
61 }
|
|
62 }
|
|
63 data( msg.toText() );
|
|
64 }
|
|
65
|
1584
|
66 public void close() throws IOException, MailException {
|
1582
|
67 write( "QUIT\r\n" );
|
|
68 String s = read();
|
|
69 if( !s.startsWith("221") )
|
1584
|
70 throw new MailException(s);
|
1582
|
71 socket.close();
|
|
72 }
|
|
73
|
1584
|
74 public String from(String address) throws IOException, MailException {
|
1582
|
75 write( "MAIL FROM: " + address + "\r\n" );
|
|
76 String r = read();
|
|
77 if( !r.startsWith("250") )
|
1584
|
78 throw new MailException(r);
|
1582
|
79 return r;
|
|
80 }
|
|
81
|
1584
|
82 public String to(String address) throws IOException, MailException {
|
1582
|
83 write( "RCPT TO: " + address + "\r\n" );
|
|
84 String r = read();
|
|
85 if( !r.startsWith("250") )
|
1584
|
86 throw new MailException(r);
|
1582
|
87 return r;
|
|
88 }
|
|
89
|
1584
|
90 public String data(String text) throws IOException, MailException {
|
1582
|
91 if( !text.endsWith("\r\n") )
|
1584
|
92 throw new MailException("text must end with \\r\\n");
|
1582
|
93 text = text.replace("\r\n.","\r\n..");
|
|
94 write( "DATA\r\n" );
|
|
95 String r = read();
|
|
96 if( !r.startsWith("354") )
|
1584
|
97 throw new MailException(r);
|
1582
|
98 write( text + ".\r\n" );
|
|
99 r = read();
|
|
100 if( !r.startsWith("250") )
|
1584
|
101 throw new MailException(r);
|
1582
|
102 return r;
|
|
103 }
|
|
104
|
|
105 private String read() throws IOException {
|
|
106 int n = reader.read(buf);
|
|
107 return new String(buf,0,n);
|
|
108 }
|
|
109
|
|
110 private void write(String s) throws IOException {
|
|
111 writer.write(s);
|
|
112 writer.flush();
|
|
113 }
|
|
114 }
|