comparison src/goodjava/mail/Message.java @ 1584:d3728e3e5af3

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 11 Mar 2021 01:22:20 -0700
parents 1cc6c7fa803d
children c0ef8acf069d
comparison
equal deleted inserted replaced
1583:1cc6c7fa803d 1584:d3728e3e5af3
1 package goodjava.mail; 1 package goodjava.mail;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 import java.util.LinkedHashMap; 4 import java.util.Base64;
5 import java.util.regex.Pattern; 5 import java.util.regex.Pattern;
6 import java.util.regex.Matcher; 6 import java.util.regex.Matcher;
7 import goodjava.util.GoodUtils; 7 import goodjava.util.GoodUtils;
8 8
9 import java.net.Socket;
10
11 9
12 public class Message { 10 public class Message {
13 public final Map<String,String> headers = new LinkedHashMap<String,String>(); 11 public final Map<String,String> headers;
14 public final String content; 12 public final Object content;
15 public final String contentType;
16 private static Pattern line = Pattern.compile("(?m)^.*$"); 13 private static Pattern line = Pattern.compile("(?m)^.*$");
17 14
18 public Message(String content) { 15 public Message(Map<String,String> headers,Object content) {
19 this(content,"text/plain"); 16 this.headers = headers;
17 this.content = content;
20 } 18 }
21 19
22 public Message(String content,String contentType) { 20 private static void addBase64(StringBuilder sb,byte[] a) {
23 this.content = content; 21 String s = Base64.getEncoder().encodeToString(a);
24 this.contentType = contentType; 22 int n = s.length() - 76;
23 int i;
24 for( i=0; i<n; i+=76 ) {
25 sb.append(s.substring(i,i+76)).append("\r\n");
26 }
27 sb.append(s.substring(i)).append("\r\n");
25 } 28 }
26 29
27 public String toText() { 30 public String toText() throws MailException {
28 StringBuilder sb = new StringBuilder(); 31 StringBuilder sb = new StringBuilder();
32 String contentType = null;
29 for( Map.Entry<String,String> entry : headers.entrySet() ) { 33 for( Map.Entry<String,String> entry : headers.entrySet() ) {
30 String name = entry.getKey(); 34 String name = entry.getKey();
35 String value = entry.getValue();
31 if( name.equalsIgnoreCase("bcc") ) 36 if( name.equalsIgnoreCase("bcc") )
32 continue; 37 continue;
33 String value = entry.getValue(); 38 if( name.equalsIgnoreCase("content-type") ) {
39 contentType = value;
40 continue;
41 }
34 sb.append( name ).append( ": " ).append( value ).append( "\r\n" ); 42 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
35 } 43 }
36 sb.append( "Content-Type: " ).append( contentType ).append( "; charset=\"UTF-8\"\r\n" ); 44 if( contentType==null )
37 boolean isAscii = content.matches("\\p{ASCII}*"); 45 throw new MailException("Content-Type not defined");
38 if( !isAscii ) 46 if( content instanceof String ) {
47 String s = (String)content;
48 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
49 boolean isAscii = s.matches("\\p{ASCII}*");
50 if( !isAscii )
51 sb.append( "Content-Transfer-Encoding: base64\r\n" );
52 sb.append( "\r\n" );
53 if( isAscii ) {
54 Matcher m = line.matcher(s);
55 while( m.find() ) {
56 sb.append(m.group()).append("\r\n");
57 }
58 } else {
59 addBase64( sb, GoodUtils.getBytes(s,"UTF-8") );
60 }
61 } else if( content instanceof byte[] ) {
62 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
39 sb.append( "Content-Transfer-Encoding: base64\r\n" ); 63 sb.append( "Content-Transfer-Encoding: base64\r\n" );
40 sb.append( "\r\n" ); 64 sb.append( "\r\n" );
41 if( isAscii ) { 65 addBase64( sb, (byte[])content );
42 Matcher m = line.matcher(content); 66 } else
43 while( m.find() ) { 67 throw new MailException("content is unrecognized type");
44 sb.append(m.group()).append("\r\n");
45 }
46 } else {
47 String s = GoodUtils.base64Encode(content);
48 int n = s.length() - 76;
49 int i;
50 for( i=0; i<n; i+=76 ) {
51 sb.append(s.substring(i,i+76)).append("\r\n");
52 }
53 sb.append(s.substring(i)).append("\r\n");
54 }
55 return sb.toString(); 68 return sb.toString();
56 } 69 }
57
58 public static void main(String[] args) throws Exception {
59 String text = ""
60 +"testm 2\n"
61 +"x\n"
62 // +"产品\n"
63 +"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"
64 ;
65 Message msg = new Message(text);
66 msg.headers.put("From","smtp@luan.software");
67 msg.headers.put("To","fschmidt@gmail.com");
68 msg.headers.put("Subject","test");
69 Socket socket = new Socket("smtpcorp.com",2525);
70 Smtp smtp = new Smtp(socket);
71 smtp.authenticate("smtp@luan.software","luanhost");
72 smtp.send(msg);
73 smtp.close();
74 System.out.println("done msg");
75 }
76 } 70 }