1583
|
1 package goodjava.mail;
|
|
2
|
|
3 import java.util.Map;
|
|
4 import java.util.LinkedHashMap;
|
|
5 import java.util.regex.Pattern;
|
|
6 import java.util.regex.Matcher;
|
|
7 import goodjava.util.GoodUtils;
|
|
8
|
|
9 import java.net.Socket;
|
|
10
|
|
11
|
|
12 public class Message {
|
|
13 public final Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
14 public final String content;
|
|
15 public final String contentType;
|
|
16 private static Pattern line = Pattern.compile("(?m)^.*$");
|
|
17
|
|
18 public Message(String content) {
|
|
19 this(content,"text/plain");
|
|
20 }
|
|
21
|
|
22 public Message(String content,String contentType) {
|
|
23 this.content = content;
|
|
24 this.contentType = contentType;
|
|
25 }
|
|
26
|
|
27 public String toText() {
|
|
28 StringBuilder sb = new StringBuilder();
|
|
29 for( Map.Entry<String,String> entry : headers.entrySet() ) {
|
|
30 String name = entry.getKey();
|
|
31 if( name.equalsIgnoreCase("bcc") )
|
|
32 continue;
|
|
33 String value = entry.getValue();
|
|
34 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
|
|
35 }
|
|
36 sb.append( "Content-Type: " ).append( contentType ).append( "; charset=\"UTF-8\"\r\n" );
|
|
37 boolean isAscii = content.matches("\\p{ASCII}*");
|
|
38 if( !isAscii )
|
|
39 sb.append( "Content-Transfer-Encoding: base64\r\n" );
|
|
40 sb.append( "\r\n" );
|
|
41 if( isAscii ) {
|
|
42 Matcher m = line.matcher(content);
|
|
43 while( m.find() ) {
|
|
44 sb.append(m.group()).append("\r\n");
|
|
45 }
|
|
46 } else {
|
|
47 String s = GoodUtils.base64Encode(content);
|
|
48 int n = s.length() - 76;
|
|
49 int i;
|
|
50 for( i=0; i<n; i+=76 ) {
|
|
51 sb.append(s.substring(i,i+76)).append("\r\n");
|
|
52 }
|
|
53 sb.append(s.substring(i)).append("\r\n");
|
|
54 }
|
|
55 return sb.toString();
|
|
56 }
|
|
57
|
|
58 public static void main(String[] args) throws Exception {
|
|
59 String text = ""
|
|
60 +"testm 2\n"
|
|
61 +"x\n"
|
|
62 // +"产品\n"
|
|
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"
|
|
64 ;
|
|
65 Message msg = new Message(text);
|
|
66 msg.headers.put("From","smtp@luan.software");
|
|
67 msg.headers.put("To","fschmidt@gmail.com");
|
|
68 msg.headers.put("Subject","test");
|
|
69 Socket socket = new Socket("smtpcorp.com",2525);
|
|
70 Smtp smtp = new Smtp(socket);
|
|
71 smtp.authenticate("smtp@luan.software","luanhost");
|
|
72 smtp.send(msg);
|
|
73 smtp.close();
|
|
74 System.out.println("done msg");
|
|
75 }
|
|
76 }
|