Mercurial Hosting > luan
comparison src/goodjava/mail/Smtp.java @ 1584:d3728e3e5af3
mail work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 11 Mar 2021 01:22:20 -0700 |
parents | 1cc6c7fa803d |
children | e34b73678a4f |
comparison
equal
deleted
inserted
replaced
1583:1cc6c7fa803d | 1584:d3728e3e5af3 |
---|---|
15 private final Socket socket; | 15 private final Socket socket; |
16 private final Reader reader; | 16 private final Reader reader; |
17 private final Writer writer; | 17 private final Writer writer; |
18 public final String ehlo; | 18 public final String ehlo; |
19 | 19 |
20 public Smtp(Socket socket) throws IOException, SmtpException { | 20 public Smtp(Socket socket) throws IOException, MailException { |
21 this.socket = socket; | 21 this.socket = socket; |
22 this.reader = new InputStreamReader(socket.getInputStream()); | 22 this.reader = new InputStreamReader(socket.getInputStream()); |
23 this.writer = new OutputStreamWriter(socket.getOutputStream()); | 23 this.writer = new OutputStreamWriter(socket.getOutputStream()); |
24 String s = read(); | 24 String s = read(); |
25 if( !s.startsWith("220") ) | 25 if( !s.startsWith("220") ) |
26 throw new SmtpException(s); | 26 throw new MailException(s); |
27 write( "EHLO\r\n" ); | 27 write( "EHLO\r\n" ); |
28 ehlo = read(); | 28 ehlo = read(); |
29 if( !ehlo.startsWith("250") ) | 29 if( !ehlo.startsWith("250") ) |
30 throw new SmtpException(ehlo); | 30 throw new MailException(ehlo); |
31 } | 31 } |
32 | 32 |
33 public String authenticate(String username,String password) throws IOException, SmtpException { | 33 public String authenticate(String username,String password) throws IOException, MailException { |
34 String s = "\0" + username + "\0" + password; | 34 String s = "\0" + username + "\0" + password; |
35 s = GoodUtils.base64Encode(s); | 35 s = GoodUtils.base64Encode(s); |
36 write( "AUTH PLAIN " + s + "\r\n" ); | 36 write( "AUTH PLAIN " + s + "\r\n" ); |
37 String r = read(); | 37 String r = read(); |
38 if( !r.startsWith("235") ) | 38 if( !r.startsWith("235") ) |
39 throw new SmtpException(r); | 39 throw new MailException(r); |
40 return r; | 40 return r; |
41 } | 41 } |
42 | 42 |
43 public void send(Message msg) throws IOException, SmtpException { | 43 public void send(Message msg) throws IOException, MailException { |
44 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) { | 44 for( Map.Entry<String,String> entry : msg.headers.entrySet() ) { |
45 String name = entry.getKey(); | 45 String name = entry.getKey(); |
46 String value = entry.getValue(); | 46 String value = entry.getValue(); |
47 if( name.equalsIgnoreCase("from") ) { | 47 if( name.equalsIgnoreCase("from") ) { |
48 from(value); | 48 from(value); |
57 } | 57 } |
58 } | 58 } |
59 data( msg.toText() ); | 59 data( msg.toText() ); |
60 } | 60 } |
61 | 61 |
62 public void close() throws IOException, SmtpException { | 62 public void close() throws IOException, MailException { |
63 write( "QUIT\r\n" ); | 63 write( "QUIT\r\n" ); |
64 String s = read(); | 64 String s = read(); |
65 if( !s.startsWith("221") ) | 65 if( !s.startsWith("221") ) |
66 throw new SmtpException(s); | 66 throw new MailException(s); |
67 socket.close(); | 67 socket.close(); |
68 } | 68 } |
69 | 69 |
70 public String from(String address) throws IOException, SmtpException { | 70 public String from(String address) throws IOException, MailException { |
71 write( "MAIL FROM: " + address + "\r\n" ); | 71 write( "MAIL FROM: " + address + "\r\n" ); |
72 String r = read(); | 72 String r = read(); |
73 if( !r.startsWith("250") ) | 73 if( !r.startsWith("250") ) |
74 throw new SmtpException(r); | 74 throw new MailException(r); |
75 return r; | 75 return r; |
76 } | 76 } |
77 | 77 |
78 public String to(String address) throws IOException, SmtpException { | 78 public String to(String address) throws IOException, MailException { |
79 write( "RCPT TO: " + address + "\r\n" ); | 79 write( "RCPT TO: " + address + "\r\n" ); |
80 String r = read(); | 80 String r = read(); |
81 if( !r.startsWith("250") ) | 81 if( !r.startsWith("250") ) |
82 throw new SmtpException(r); | 82 throw new MailException(r); |
83 return r; | 83 return r; |
84 } | 84 } |
85 | 85 |
86 public String data(String text) throws IOException, SmtpException { | 86 public String data(String text) throws IOException, MailException { |
87 if( !text.endsWith("\r\n") ) | 87 if( !text.endsWith("\r\n") ) |
88 throw new SmtpException("text must end with \\r\\n"); | 88 throw new MailException("text must end with \\r\\n"); |
89 text = text.replace("\r\n.","\r\n.."); | 89 text = text.replace("\r\n.","\r\n.."); |
90 write( "DATA\r\n" ); | 90 write( "DATA\r\n" ); |
91 String r = read(); | 91 String r = read(); |
92 if( !r.startsWith("354") ) | 92 if( !r.startsWith("354") ) |
93 throw new SmtpException(r); | 93 throw new MailException(r); |
94 write( text + ".\r\n" ); | 94 write( text + ".\r\n" ); |
95 r = read(); | 95 r = read(); |
96 if( !r.startsWith("250") ) | 96 if( !r.startsWith("250") ) |
97 throw new SmtpException(r); | 97 throw new MailException(r); |
98 return r; | 98 return r; |
99 } | 99 } |
100 | 100 |
101 private String read() throws IOException { | 101 private String read() throws IOException { |
102 int n = reader.read(buf); | 102 int n = reader.read(buf); |
105 | 105 |
106 private void write(String s) throws IOException { | 106 private void write(String s) throws IOException { |
107 writer.write(s); | 107 writer.write(s); |
108 writer.flush(); | 108 writer.flush(); |
109 } | 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"); | |
116 smtp.to(" fschmidt@gmail.com"); | |
117 String text = "\r\n" | |
118 +"test3\r\n" | |
119 +".q\r\n" | |
120 +"x\r\n" | |
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" | |
122 ; | |
123 smtp.data(text); | |
124 smtp.close(); | |
125 } | |
126 } | 110 } |