Mercurial Hosting > luan
comparison src/goodjava/mail/Smtp.java @ 1582:f28cc30d56cb
start goodjava/mail
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 06 Mar 2021 21:13:34 -0700 |
parents | |
children | 1cc6c7fa803d |
comparison
equal
deleted
inserted
replaced
1581:dbf63bef4b86 | 1582:f28cc30d56cb |
---|---|
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; | |
9 import java.util.Base64; | |
10 | |
11 | |
12 public class Smtp { | |
13 private final char[] buf = new char[1000]; | |
14 private final Socket socket; | |
15 private final Reader reader; | |
16 private final Writer writer; | |
17 public final String ehlo; | |
18 | |
19 public Smtp(Socket socket) throws IOException, SmtpException { | |
20 this.socket = socket; | |
21 this.reader = new InputStreamReader(socket.getInputStream()); | |
22 this.writer = new OutputStreamWriter(socket.getOutputStream()); | |
23 String s = read(); | |
24 if( !s.startsWith("220") ) | |
25 throw new SmtpException(s); | |
26 write( "EHLO\r\n" ); | |
27 ehlo = read(); | |
28 if( !ehlo.startsWith("250") ) | |
29 throw new SmtpException(ehlo); | |
30 } | |
31 | |
32 public void close() throws IOException, SmtpException { | |
33 write( "QUIT\r\n" ); | |
34 String s = read(); | |
35 if( !s.startsWith("221") ) | |
36 throw new SmtpException(s); | |
37 socket.close(); | |
38 } | |
39 | |
40 public String authenticate(String username,String password) throws IOException, SmtpException { | |
41 String s = "\0" + username + "\0" + password; | |
42 s = Base64.getEncoder().encodeToString(s.getBytes()); | |
43 write( "AUTH PLAIN " + s + "\r\n" ); | |
44 String r = read(); | |
45 if( !r.startsWith("235") ) | |
46 throw new SmtpException(r); | |
47 return r; | |
48 } | |
49 | |
50 public String from(String address) throws IOException, SmtpException { | |
51 write( "MAIL FROM: " + address + "\r\n" ); | |
52 String r = read(); | |
53 if( !r.startsWith("250") ) | |
54 throw new SmtpException(r); | |
55 return r; | |
56 } | |
57 | |
58 public String to(String address) throws IOException, SmtpException { | |
59 write( "RCPT TO: " + address + "\r\n" ); | |
60 String r = read(); | |
61 if( !r.startsWith("250") ) | |
62 throw new SmtpException(r); | |
63 return r; | |
64 } | |
65 | |
66 public String data(String text) throws IOException, SmtpException { | |
67 if( !text.endsWith("\r\n") ) | |
68 throw new SmtpException("text must end with \\r\\n"); | |
69 text = text.replace("\r\n.","\r\n.."); | |
70 write( "DATA\r\n" ); | |
71 String r = read(); | |
72 if( !r.startsWith("354") ) | |
73 throw new SmtpException(r); | |
74 write( text + ".\r\n" ); | |
75 r = read(); | |
76 if( !r.startsWith("250") ) | |
77 throw new SmtpException(r); | |
78 return r; | |
79 } | |
80 | |
81 private String read() throws IOException { | |
82 int n = reader.read(buf); | |
83 return new String(buf,0,n); | |
84 } | |
85 | |
86 private void write(String s) throws IOException { | |
87 writer.write(s); | |
88 writer.flush(); | |
89 } | |
90 | |
91 public static void main(String[] args) throws Exception { | |
92 Socket socket = new Socket("smtpcorp.com",2525); | |
93 Smtp smtp = new Smtp(socket); | |
94 smtp.authenticate("smtp@luan.software","luanhost"); | |
95 smtp.from("smtp@luan.software"); | |
96 smtp.to("fschmidt@gmail.com"); | |
97 String text = "\r\n" | |
98 +"test2\r\n" | |
99 +".q\r\n" | |
100 +"x\r\n" | |
101 ; | |
102 smtp.data(text); | |
103 smtp.close(); | |
104 } | |
105 } |