annotate src/goodjava/mail/Examples.java @ 1831:8f9ae295bf6a default tip

add Hosted.authorize
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 26 Sep 2024 15:07:45 -0600
parents d72a52232f79
children
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);
1830
d72a52232f79 mail - minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1829
diff changeset
15 Smtp smtp = new Smtp(socket);
1824
92860f763715 smtpcorp change
Franklin Schmidt <fschmidt@gmail.com>
parents: 1787
diff changeset
16 smtp.authenticate("luan","luanhost2");
1584
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 +"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
39 ;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 Map<String,String> headers = new LinkedHashMap<String,String>();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 headers.put("From","smtp@luan.software");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 headers.put("To","fschmidt@gmail.com");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 headers.put("Subject","test text");
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
44 // headers.put("Content-Type","text/plain; charset=utf-8");
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 Message msg = new Message(headers,text);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 Smtp smtp = newSmtp();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 smtp.send(msg);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 smtp.close();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 private static byte[] flag() throws IOException {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 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
53 InputStream in = url.openConnection().getInputStream();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 byte[] a = new byte[100000];
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 int n = 0;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 int i;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 while( (i=in.read(a,n,a.length-n)) != -1 )
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 n += i;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 in.close();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 byte[] rtn = new byte[n];
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 System.arraycopy(a,0,rtn,0,n);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 return rtn;
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 private static void mailFlag() throws IOException, MailException {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 byte[] flag = flag();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 Map<String,String> headers = new LinkedHashMap<String,String>();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 headers.put("From","smtp@luan.software");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 headers.put("To","fschmidt@gmail.com");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 headers.put("Subject","test flag");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 headers.put("Content-Type","image/png; name=\"flag.png\"");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 Message msg = new Message(headers,flag);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 Smtp smtp = newSmtp();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 smtp.send(msg);
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 smtp.close();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
1585
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
78 private static void multi() throws IOException, MailException {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
79 Message msg1;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
80 {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
81 String text = ""
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
82 +"testing 1\n"
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
83 +"more\n"
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
84 ;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
85 Map<String,String> headers = new LinkedHashMap<String,String>();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
86 headers.put("Content-Type","text/plain; charset=utf-8");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
87 msg1 = new Message(headers,text);
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
88 }
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 Message msg2;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
91 {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
92 String text = ""
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
93 +"testing 2\n"
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
94 +"more\n"
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
95 ;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
96 Map<String,String> headers = new LinkedHashMap<String,String>();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
97 headers.put("Content-Type","text/plain; charset=utf-8");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
98 headers.put("Content-Disposition","attachment; filename=\"t2.txt\"");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
99 msg2 = new Message(headers,text);
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
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
102 Message msg3;
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
103 {
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
104 byte[] flag = flag();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
105 Map<String,String> headers = new LinkedHashMap<String,String>();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
106 headers.put("Content-Type","image/png; name=\"flag.png\"");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
107 // headers.put("Content-Type","image/png");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
108 // headers.put("Content-Disposition","inline");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
109 // headers.put("Content-Disposition","attachment; filename=\"flag.png\"");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
110 msg3 = new Message(headers,flag);
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
111 }
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 Map<String,String> headers = new LinkedHashMap<String,String>();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
114 headers.put("From","smtp@luan.software");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
115 headers.put("To","fschmidt@gmail.com");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
116 headers.put("Subject","multi");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
117 headers.put("Content-Type","multipart/mixed");
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
118 Message[] content = new Message[]{msg1,msg2,msg3};
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
119 Message msg = new Message(headers,content);
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
120 Smtp smtp = newSmtp();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
121 smtp.send(msg);
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
122 smtp.close();
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
123 }
c0ef8acf069d multipart mail
Franklin Schmidt <fschmidt@gmail.com>
parents: 1584
diff changeset
124
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 public static void main(String[] args) throws Exception {
1587
fa1a9aceac3e mail work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1585
diff changeset
126 mailText();
1584
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 }