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

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