comparison src/goodjava/mail/Message.java @ 1587:fa1a9aceac3e

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 23:22:53 -0700
parents c0ef8acf069d
children 0b904d30721f
comparison
equal deleted inserted replaced
1586:fcca0ddf5a4d 1587:fa1a9aceac3e
11 public class Message { 11 public class Message {
12 final Map<String,String> headers; 12 final Map<String,String> headers;
13 final Object content; 13 final Object content;
14 private static Pattern line = Pattern.compile("(?m)^.*$"); 14 private static Pattern line = Pattern.compile("(?m)^.*$");
15 15
16 public Message(Map<String,String> headers,Object content) { 16 public Message(Map<String,String> headers,String content) {
17 this.headers = headers;
18 this.content = content;
19 }
20
21 public Message(Map<String,String> headers,byte[] content) {
22 this.headers = headers;
23 this.content = content;
24 }
25
26 public Message(Map<String,String> headers,Message[] content) {
17 this.headers = headers; 27 this.headers = headers;
18 this.content = content; 28 this.content = content;
19 } 29 }
20 30
21 private static void addBase64(StringBuilder sb,byte[] a) { 31 private static void addBase64(StringBuilder sb,byte[] a) {
40 contentType = value; 50 contentType = value;
41 continue; 51 continue;
42 } 52 }
43 sb.append( name ).append( ": " ).append( value ).append( "\r\n" ); 53 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
44 } 54 }
45 if( contentType==null )
46 throw new MailException("Content-Type not defined");
47 if( content instanceof String ) { 55 if( content instanceof String ) {
48 String s = (String)content; 56 String s = (String)content;
49 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" ); 57 if( contentType!=null )
58 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
50 boolean isAscii = s.matches("\\p{ASCII}*"); 59 boolean isAscii = s.matches("\\p{ASCII}*");
51 if( !isAscii ) 60 if( !isAscii )
52 sb.append( "Content-Transfer-Encoding: base64\r\n" ); 61 sb.append( "Content-Transfer-Encoding: base64\r\n" );
53 sb.append( "\r\n" ); 62 sb.append( "\r\n" );
54 if( isAscii ) { 63 if( isAscii ) {
58 } 67 }
59 } else { 68 } else {
60 addBase64( sb, GoodUtils.getBytes(s,"UTF-8") ); 69 addBase64( sb, GoodUtils.getBytes(s,"UTF-8") );
61 } 70 }
62 } else if( content instanceof byte[] ) { 71 } else if( content instanceof byte[] ) {
63 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" ); 72 if( contentType!=null )
73 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
64 sb.append( "Content-Transfer-Encoding: base64\r\n" ); 74 sb.append( "Content-Transfer-Encoding: base64\r\n" );
65 sb.append( "\r\n" ); 75 sb.append( "\r\n" );
66 addBase64( sb, (byte[])content ); 76 addBase64( sb, (byte[])content );
67 } else if( content instanceof Message[] ) { 77 } else if( content instanceof Message[] ) {
78 if( contentType==null )
79 throw new MailException("Content-Type must be defined for multipart");
68 Message[] messages = (Message[])content; 80 Message[] messages = (Message[])content;
69 String[] texts = new String[messages.length]; 81 String[] texts = new String[messages.length];
70 StringBuilder allTextSb = new StringBuilder(); 82 StringBuilder allTextSb = new StringBuilder();
71 for( int i=0; i<messages.length; i++ ) { 83 for( int i=0; i<messages.length; i++ ) {
72 String text = messages[i].toText(); 84 String text = messages[i].toText();
86 sb.append( text ); 98 sb.append( text );
87 sb.append( "\r\n" ); 99 sb.append( "\r\n" );
88 } 100 }
89 sb.append( "--" ).append( boundary ).append( "--\r\n" ); 101 sb.append( "--" ).append( boundary ).append( "--\r\n" );
90 } else 102 } else
91 throw new MailException("content is unrecognized type: "+content.getClass()); 103 throw new RuntimeException();
92 return sb.toString(); 104 return sb.toString();
93 } 105 }
94 } 106 }