comparison src/goodjava/mail/Smtp.java @ 1583:1cc6c7fa803d

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Mar 2021 02:22:09 -0700
parents f28cc30d56cb
children d3728e3e5af3
comparison
equal deleted inserted replaced
1582:f28cc30d56cb 1583:1cc6c7fa803d
4 import java.io.InputStreamReader; 4 import java.io.InputStreamReader;
5 import java.io.Writer; 5 import java.io.Writer;
6 import java.io.OutputStreamWriter; 6 import java.io.OutputStreamWriter;
7 import java.io.IOException; 7 import java.io.IOException;
8 import java.net.Socket; 8 import java.net.Socket;
9 import java.util.Base64; 9 import java.util.Map;
10 import goodjava.util.GoodUtils;
10 11
11 12
12 public class Smtp { 13 public class Smtp {
13 private final char[] buf = new char[1000]; 14 private final char[] buf = new char[1000];
14 private final Socket socket; 15 private final Socket socket;
27 ehlo = read(); 28 ehlo = read();
28 if( !ehlo.startsWith("250") ) 29 if( !ehlo.startsWith("250") )
29 throw new SmtpException(ehlo); 30 throw new SmtpException(ehlo);
30 } 31 }
31 32
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
32 public void close() throws IOException, SmtpException { 62 public void close() throws IOException, SmtpException {
33 write( "QUIT\r\n" ); 63 write( "QUIT\r\n" );
34 String s = read(); 64 String s = read();
35 if( !s.startsWith("221") ) 65 if( !s.startsWith("221") )
36 throw new SmtpException(s); 66 throw new SmtpException(s);
37 socket.close(); 67 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 } 68 }
49 69
50 public String from(String address) throws IOException, SmtpException { 70 public String from(String address) throws IOException, SmtpException {
51 write( "MAIL FROM: " + address + "\r\n" ); 71 write( "MAIL FROM: " + address + "\r\n" );
52 String r = read(); 72 String r = read();
91 public static void main(String[] args) throws Exception { 111 public static void main(String[] args) throws Exception {
92 Socket socket = new Socket("smtpcorp.com",2525); 112 Socket socket = new Socket("smtpcorp.com",2525);
93 Smtp smtp = new Smtp(socket); 113 Smtp smtp = new Smtp(socket);
94 smtp.authenticate("smtp@luan.software","luanhost"); 114 smtp.authenticate("smtp@luan.software","luanhost");
95 smtp.from("smtp@luan.software"); 115 smtp.from("smtp@luan.software");
96 smtp.to("fschmidt@gmail.com"); 116 smtp.to(" fschmidt@gmail.com");
97 String text = "\r\n" 117 String text = "\r\n"
98 +"test2\r\n" 118 +"test3\r\n"
99 +".q\r\n" 119 +".q\r\n"
100 +"x\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"
101 ; 122 ;
102 smtp.data(text); 123 smtp.data(text);
103 smtp.close(); 124 smtp.close();
104 } 125 }
105 } 126 }