1583
|
1 package goodjava.mail;
|
|
2
|
|
3 import java.util.Map;
|
1584
|
4 import java.util.Base64;
|
1585
|
5 import java.util.Random;
|
1583
|
6 import java.util.regex.Pattern;
|
|
7 import java.util.regex.Matcher;
|
|
8 import goodjava.util.GoodUtils;
|
|
9
|
|
10
|
|
11 public class Message {
|
1585
|
12 final Map<String,String> headers;
|
|
13 final Object content;
|
1583
|
14 private static Pattern line = Pattern.compile("(?m)^.*$");
|
|
15
|
1584
|
16 public Message(Map<String,String> headers,Object content) {
|
|
17 this.headers = headers;
|
|
18 this.content = content;
|
1583
|
19 }
|
|
20
|
1584
|
21 private static void addBase64(StringBuilder sb,byte[] a) {
|
|
22 String s = Base64.getEncoder().encodeToString(a);
|
|
23 int n = s.length() - 76;
|
|
24 int i;
|
|
25 for( i=0; i<n; i+=76 ) {
|
|
26 sb.append(s.substring(i,i+76)).append("\r\n");
|
|
27 }
|
|
28 sb.append(s.substring(i)).append("\r\n");
|
1583
|
29 }
|
|
30
|
1584
|
31 public String toText() throws MailException {
|
1583
|
32 StringBuilder sb = new StringBuilder();
|
1584
|
33 String contentType = null;
|
1583
|
34 for( Map.Entry<String,String> entry : headers.entrySet() ) {
|
|
35 String name = entry.getKey();
|
1584
|
36 String value = entry.getValue();
|
1583
|
37 if( name.equalsIgnoreCase("bcc") )
|
|
38 continue;
|
1584
|
39 if( name.equalsIgnoreCase("content-type") ) {
|
|
40 contentType = value;
|
|
41 continue;
|
|
42 }
|
1583
|
43 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
|
|
44 }
|
1584
|
45 if( contentType==null )
|
|
46 throw new MailException("Content-Type not defined");
|
|
47 if( content instanceof String ) {
|
|
48 String s = (String)content;
|
|
49 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
|
|
50 boolean isAscii = s.matches("\\p{ASCII}*");
|
|
51 if( !isAscii )
|
|
52 sb.append( "Content-Transfer-Encoding: base64\r\n" );
|
|
53 sb.append( "\r\n" );
|
|
54 if( isAscii ) {
|
|
55 Matcher m = line.matcher(s);
|
|
56 while( m.find() ) {
|
|
57 sb.append(m.group()).append("\r\n");
|
|
58 }
|
|
59 } else {
|
|
60 addBase64( sb, GoodUtils.getBytes(s,"UTF-8") );
|
1583
|
61 }
|
1584
|
62 } else if( content instanceof byte[] ) {
|
|
63 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
|
|
64 sb.append( "Content-Transfer-Encoding: base64\r\n" );
|
|
65 sb.append( "\r\n" );
|
|
66 addBase64( sb, (byte[])content );
|
1585
|
67 } else if( content instanceof Message[] ) {
|
|
68 Message[] messages = (Message[])content;
|
|
69 String[] texts = new String[messages.length];
|
|
70 StringBuilder allTextSb = new StringBuilder();
|
|
71 for( int i=0; i<messages.length; i++ ) {
|
|
72 String text = messages[i].toText();
|
|
73 texts[i] = text;
|
|
74 allTextSb.append(text);
|
|
75 }
|
|
76 String allText = allTextSb.toString();
|
|
77 String boundary;
|
|
78 do {
|
|
79 boundary = Long.toHexString(new Random().nextLong());
|
|
80 } while( allText.contains(boundary) );
|
|
81 sb.append( "Content-Type: " ).append( contentType )
|
|
82 .append( "; boundary=\"" ).append( boundary ).append( "\"\r\n" );
|
|
83 sb.append( "\r\n" );
|
|
84 for( String text : texts ) {
|
|
85 sb.append( "--" ).append( boundary ).append( "\r\n" );
|
|
86 sb.append( text );
|
|
87 sb.append( "\r\n" );
|
|
88 }
|
|
89 sb.append( "--" ).append( boundary ).append( "--\r\n" );
|
1584
|
90 } else
|
1585
|
91 throw new MailException("content is unrecognized type: "+content.getClass());
|
1583
|
92 return sb.toString();
|
|
93 }
|
|
94 }
|