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
|
|
20 public Smtp(Socket socket) throws IOException, SmtpException {
|
|
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") )
|
|
26 throw new SmtpException(s);
|
|
27 write( "EHLO\r\n" );
|
|
28 ehlo = read();
|
|
29 if( !ehlo.startsWith("250") )
|
|
30 throw new SmtpException(ehlo);
|
|
31 }
|
|
32
|
1583
|
33 public String authenticate(String username,String password) throws IOException, SmtpException {
|
|
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") )
|
|
39 throw new SmtpException(r);
|
|
40 return r;
|
|
41 }
|
|
42
|
|
43 public void send(Message msg) throws IOException, SmtpException {
|
|
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
|
1582
|
62 public void close() throws IOException, SmtpException {
|
|
63 write( "QUIT\r\n" );
|
|
64 String s = read();
|
|
65 if( !s.startsWith("221") )
|
|
66 throw new SmtpException(s);
|
|
67 socket.close();
|
|
68 }
|
|
69
|
|
70 public String from(String address) throws IOException, SmtpException {
|
|
71 write( "MAIL FROM: " + address + "\r\n" );
|
|
72 String r = read();
|
|
73 if( !r.startsWith("250") )
|
|
74 throw new SmtpException(r);
|
|
75 return r;
|
|
76 }
|
|
77
|
|
78 public String to(String address) throws IOException, SmtpException {
|
|
79 write( "RCPT TO: " + address + "\r\n" );
|
|
80 String r = read();
|
|
81 if( !r.startsWith("250") )
|
|
82 throw new SmtpException(r);
|
|
83 return r;
|
|
84 }
|
|
85
|
|
86 public String data(String text) throws IOException, SmtpException {
|
|
87 if( !text.endsWith("\r\n") )
|
|
88 throw new SmtpException("text must end with \\r\\n");
|
|
89 text = text.replace("\r\n.","\r\n..");
|
|
90 write( "DATA\r\n" );
|
|
91 String r = read();
|
|
92 if( !r.startsWith("354") )
|
|
93 throw new SmtpException(r);
|
|
94 write( text + ".\r\n" );
|
|
95 r = read();
|
|
96 if( !r.startsWith("250") )
|
|
97 throw new SmtpException(r);
|
|
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
|
|
111 public static void main(String[] args) throws Exception {
|
|
112 Socket socket = new Socket("smtpcorp.com",2525);
|
|
113 Smtp smtp = new Smtp(socket);
|
|
114 smtp.authenticate("smtp@luan.software","luanhost");
|
|
115 smtp.from("smtp@luan.software");
|
1583
|
116 smtp.to(" fschmidt@gmail.com");
|
1582
|
117 String text = "\r\n"
|
1583
|
118 +"test3\r\n"
|
1582
|
119 +".q\r\n"
|
|
120 +"x\r\n"
|
1583
|
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
|
122 ;
|
|
123 smtp.data(text);
|
|
124 smtp.close();
|
|
125 }
|
|
126 }
|