annotate src/goodjava/mail/Examples.java @ 1584:d3728e3e5af3

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 11 Mar 2021 01:22:20 -0700
parents
children c0ef8acf069d
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");
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 headers.put("Content-Type","text/plain; charset=utf-8");
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
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 public static void main(String[] args) throws Exception {
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 mailText();
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
d3728e3e5af3 mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }