1584
|
1 package goodjava.mail;
|
|
2
|
|
3 import java.io.InputStream;
|
|
4 import java.io.IOException;
|
|
5 import java.net.Socket;
|
|
6 import java.net.URL;
|
|
7 import java.util.Map;
|
|
8 import java.util.LinkedHashMap;
|
|
9
|
|
10
|
|
11 public class Examples {
|
|
12
|
|
13 private static Smtp newSmtp() throws IOException, MailException {
|
|
14 Socket socket = new Socket("smtpcorp.com",2525);
|
|
15 Smtp smtp = new Smtp(socket);
|
|
16 smtp.authenticate("smtp@luan.software","luanhost");
|
|
17 return smtp;
|
|
18 }
|
|
19
|
|
20 private static void smtp() throws IOException, MailException {
|
|
21 Smtp smtp = newSmtp();
|
|
22 smtp.from("smtp@luan.software");
|
|
23 smtp.to("fschmidt@gmail.com");
|
|
24 String text = "\r\n"
|
|
25 +"test3\r\n"
|
|
26 +".q\r\n"
|
|
27 +"x\r\n"
|
|
28 +"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"
|
|
29 ;
|
|
30 smtp.data(text);
|
|
31 smtp.close();
|
|
32 }
|
|
33
|
|
34 private static void mailText() throws IOException, MailException {
|
|
35 String text = ""
|
|
36 +"testm 2\n"
|
|
37 +"x\n"
|
|
38 +"产品\n" // some random chinese
|
|
39 +"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\n"
|
|
40 ;
|
|
41 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
42 headers.put("From","smtp@luan.software");
|
|
43 headers.put("To","fschmidt@gmail.com");
|
|
44 headers.put("Subject","test text");
|
1587
|
45 // headers.put("Content-Type","text/plain; charset=utf-8");
|
1584
|
46 Message msg = new Message(headers,text);
|
|
47 Smtp smtp = newSmtp();
|
|
48 smtp.send(msg);
|
|
49 smtp.close();
|
|
50 }
|
|
51
|
|
52 private static byte[] flag() throws IOException {
|
|
53 URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Confederate_Rebel_Flag.svg/440px-Confederate_Rebel_Flag.svg.png");
|
|
54 InputStream in = url.openConnection().getInputStream();
|
|
55 byte[] a = new byte[100000];
|
|
56 int n = 0;
|
|
57 int i;
|
|
58 while( (i=in.read(a,n,a.length-n)) != -1 )
|
|
59 n += i;
|
|
60 in.close();
|
|
61 byte[] rtn = new byte[n];
|
|
62 System.arraycopy(a,0,rtn,0,n);
|
|
63 return rtn;
|
|
64 }
|
|
65
|
|
66 private static void mailFlag() throws IOException, MailException {
|
|
67 byte[] flag = flag();
|
|
68 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
69 headers.put("From","smtp@luan.software");
|
|
70 headers.put("To","fschmidt@gmail.com");
|
|
71 headers.put("Subject","test flag");
|
|
72 headers.put("Content-Type","image/png; name=\"flag.png\"");
|
|
73 Message msg = new Message(headers,flag);
|
|
74 Smtp smtp = newSmtp();
|
|
75 smtp.send(msg);
|
|
76 smtp.close();
|
|
77 }
|
|
78
|
1585
|
79 private static void multi() throws IOException, MailException {
|
|
80 Message msg1;
|
|
81 {
|
|
82 String text = ""
|
|
83 +"testing 1\n"
|
|
84 +"more\n"
|
|
85 ;
|
|
86 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
87 headers.put("Content-Type","text/plain; charset=utf-8");
|
|
88 msg1 = new Message(headers,text);
|
|
89 }
|
|
90
|
|
91 Message msg2;
|
|
92 {
|
|
93 String text = ""
|
|
94 +"testing 2\n"
|
|
95 +"more\n"
|
|
96 ;
|
|
97 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
98 headers.put("Content-Type","text/plain; charset=utf-8");
|
|
99 headers.put("Content-Disposition","attachment; filename=\"t2.txt\"");
|
|
100 msg2 = new Message(headers,text);
|
|
101 }
|
|
102
|
|
103 Message msg3;
|
|
104 {
|
|
105 byte[] flag = flag();
|
|
106 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
107 headers.put("Content-Type","image/png; name=\"flag.png\"");
|
|
108 // headers.put("Content-Type","image/png");
|
|
109 // headers.put("Content-Disposition","inline");
|
|
110 // headers.put("Content-Disposition","attachment; filename=\"flag.png\"");
|
|
111 msg3 = new Message(headers,flag);
|
|
112 }
|
|
113
|
|
114 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
115 headers.put("From","smtp@luan.software");
|
|
116 headers.put("To","fschmidt@gmail.com");
|
|
117 headers.put("Subject","multi");
|
|
118 headers.put("Content-Type","multipart/mixed");
|
|
119 Message[] content = new Message[]{msg1,msg2,msg3};
|
|
120 Message msg = new Message(headers,content);
|
|
121 Smtp smtp = newSmtp();
|
|
122 smtp.send(msg);
|
|
123 smtp.close();
|
|
124 }
|
|
125
|
1584
|
126 public static void main(String[] args) throws Exception {
|
1587
|
127 mailText();
|
1584
|
128 }
|
|
129 }
|