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