comparison src/goodjava/mail/Message.java @ 1585:c0ef8acf069d

multipart mail
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 18:06:15 -0700
parents d3728e3e5af3
children fa1a9aceac3e
comparison
equal deleted inserted replaced
1584:d3728e3e5af3 1585:c0ef8acf069d
1 package goodjava.mail; 1 package goodjava.mail;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 import java.util.Base64; 4 import java.util.Base64;
5 import java.util.Random;
5 import java.util.regex.Pattern; 6 import java.util.regex.Pattern;
6 import java.util.regex.Matcher; 7 import java.util.regex.Matcher;
7 import goodjava.util.GoodUtils; 8 import goodjava.util.GoodUtils;
8 9
9 10
10 public class Message { 11 public class Message {
11 public final Map<String,String> headers; 12 final Map<String,String> headers;
12 public final Object content; 13 final Object content;
13 private static Pattern line = Pattern.compile("(?m)^.*$"); 14 private static Pattern line = Pattern.compile("(?m)^.*$");
14 15
15 public Message(Map<String,String> headers,Object content) { 16 public Message(Map<String,String> headers,Object content) {
16 this.headers = headers; 17 this.headers = headers;
17 this.content = content; 18 this.content = content;
61 } else if( content instanceof byte[] ) { 62 } else if( content instanceof byte[] ) {
62 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" ); 63 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
63 sb.append( "Content-Transfer-Encoding: base64\r\n" ); 64 sb.append( "Content-Transfer-Encoding: base64\r\n" );
64 sb.append( "\r\n" ); 65 sb.append( "\r\n" );
65 addBase64( sb, (byte[])content ); 66 addBase64( sb, (byte[])content );
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" );
66 } else 90 } else
67 throw new MailException("content is unrecognized type"); 91 throw new MailException("content is unrecognized type: "+content.getClass());
68 return sb.toString(); 92 return sb.toString();
69 } 93 }
70 } 94 }