annotate src/goodjava/mail/Message.java @ 1583:1cc6c7fa803d

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Mar 2021 02:22:09 -0700
parents
children d3728e3e5af3
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;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.LinkedHashMap;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.regex.Pattern;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.regex.Matcher;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import goodjava.util.GoodUtils;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.net.Socket;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public class Message {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public final Map<String,String> headers = new LinkedHashMap<String,String>();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public final String content;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public final String contentType;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private static Pattern line = Pattern.compile("(?m)^.*$");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public Message(String content) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 this(content,"text/plain");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 public Message(String content,String contentType) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.content = content;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 this.contentType = contentType;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 public String toText() {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 StringBuilder sb = new StringBuilder();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 for( Map.Entry<String,String> entry : headers.entrySet() ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 String name = entry.getKey();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 if( name.equalsIgnoreCase("bcc") )
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 continue;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 String value = entry.getValue();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 sb.append( "Content-Type: " ).append( contentType ).append( "; charset=\"UTF-8\"\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 boolean isAscii = content.matches("\\p{ASCII}*");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 if( !isAscii )
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 sb.append( "Content-Transfer-Encoding: base64\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 sb.append( "\r\n" );
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 if( isAscii ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 Matcher m = line.matcher(content);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 while( m.find() ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 sb.append(m.group()).append("\r\n");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 } else {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 String s = GoodUtils.base64Encode(content);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 int n = s.length() - 76;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 int i;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 for( i=0; i<n; i+=76 ) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 sb.append(s.substring(i,i+76)).append("\r\n");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 sb.append(s.substring(i)).append("\r\n");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return sb.toString();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 public static void main(String[] args) throws Exception {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 String text = ""
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 +"testm 2\n"
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 +"x\n"
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 // +"产品\n"
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 +"rg; ;lrg dsl rgj errlgerrg neskrjg skrg rdsg drskrg sd;gr s;kgr skrg skrg sdg ds fg;ks gegr erg ;sg sd; g;sdr gsklrg sg s;kkrg s;hg ;slrg ;elrg ;reg r;g ;r g;er g;ler g;e g; g;r g rg; srkd fjl kj kklsjrg lsk gskdf;rs gkrj glj grekjs lksjgkjn kjslg rklrg ;rsd; kj drsg akrglk kalrgklrsdnrgkgj;r ;s ns b;n;sn ;njslk r;n\r\n"
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 ;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 Message msg = new Message(text);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 msg.headers.put("From","smtp@luan.software");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 msg.headers.put("To","fschmidt@gmail.com");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 msg.headers.put("Subject","test");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 Socket socket = new Socket("smtpcorp.com",2525);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 Smtp smtp = new Smtp(socket);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 smtp.authenticate("smtp@luan.software","luanhost");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 smtp.send(msg);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 smtp.close();
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 System.out.println("done msg");
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }