annotate src/goodjava/mail/Message.java @ 1652:d5779a264a4a

docs work
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 31 Mar 2022 19:04:59 -0600
parents 0b904d30721f
children
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;
1588
Franklin Schmidt <fschmidt@gmail.com>
parents: 1587
diff changeset
14 private static final Pattern line = Pattern.compile("(?m)^.*$");
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
16 public Message(Map<String,String> headers,String content) {
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
17 this.headers = headers;
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
18 this.content = content;
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
19 }
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
20
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
21 public Message(Map<String,String> headers,byte[] content) {
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
22 this.headers = headers;
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
23 this.content = content;
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
24 }
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
25
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
26 public Message(Map<String,String> headers,Message[] content) {
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
27 this.headers = headers;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
28 this.content = content;
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 private static void addBase64(StringBuilder sb,byte[] a) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
32 String s = Base64.getEncoder().encodeToString(a);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
33 int n = s.length() - 76;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
34 int i;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
35 for( i=0; i<n; i+=76 ) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
36 sb.append(s.substring(i,i+76)).append("\r\n");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
37 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
38 sb.append(s.substring(i)).append("\r\n");
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
41 public String toText() throws MailException {
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 StringBuilder sb = new StringBuilder();
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
43 String contentType = null;
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 for( Map.Entry<String,String> entry : headers.entrySet() ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 String name = entry.getKey();
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
46 String value = entry.getValue();
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if( name.equalsIgnoreCase("bcc") )
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 continue;
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
49 if( name.equalsIgnoreCase("content-type") ) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
50 contentType = value;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
51 continue;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
52 }
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
55 if( content instanceof String ) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
56 String s = (String)content;
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
57 if( contentType!=null )
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
58 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
59 boolean isAscii = s.matches("\\p{ASCII}*");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
60 if( !isAscii )
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
61 sb.append( "Content-Transfer-Encoding: base64\r\n" );
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
62 sb.append( "\r\n" );
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
63 if( isAscii ) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
64 Matcher m = line.matcher(s);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
65 while( m.find() ) {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
66 sb.append(m.group()).append("\r\n");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
67 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
68 } else {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
69 addBase64( sb, GoodUtils.getBytes(s,"UTF-8") );
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
71 } else if( content instanceof byte[] ) {
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
72 if( contentType!=null )
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
73 sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
74 sb.append( "Content-Transfer-Encoding: base64\r\n" );
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
75 sb.append( "\r\n" );
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
76 addBase64( sb, (byte[])content );
1585
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
77 } else if( content instanceof Message[] ) {
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
78 if( contentType==null )
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
79 throw new MailException("Content-Type must be defined for multipart");
1585
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
80 Message[] messages = (Message[])content;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
81 String[] texts = new String[messages.length];
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
82 StringBuilder allTextSb = new StringBuilder();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
83 for( int i=0; i<messages.length; i++ ) {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
84 String text = messages[i].toText();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
85 texts[i] = text;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
86 allTextSb.append(text);
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
87 }
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
88 String allText = allTextSb.toString();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
89 String boundary;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
90 do {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
91 boundary = Long.toHexString(new Random().nextLong());
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
92 } while( allText.contains(boundary) );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
93 sb.append( "Content-Type: " ).append( contentType )
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
94 .append( "; boundary=\"" ).append( boundary ).append( "\"\r\n" );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
95 sb.append( "\r\n" );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
96 for( String text : texts ) {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
97 sb.append( "--" ).append( boundary ).append( "\r\n" );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
98 sb.append( text );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
99 sb.append( "\r\n" );
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
100 }
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
101 sb.append( "--" ).append( boundary ).append( "--\r\n" );
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
102 } else
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
103 throw new RuntimeException();
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 return sb.toString();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }