comparison src/luan/modules/mail/MailSender.java @ 1589:0c46edec25dd

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 13 Mar 2021 21:02:38 -0700
parents src/luan/modules/mail/MailCon.java@fa1a9aceac3e
children 1ffe1e06ea55
comparison
equal deleted inserted replaced
1588:0b904d30721f 1589:0c46edec25dd
1 package luan.modules.mail;
2
3 import java.io.IOException;
4 import java.net.Socket;
5 import java.util.Map;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import goodjava.mail.Message;
9 import goodjava.mail.Smtp;
10 import goodjava.mail.MailException;
11 import luan.Luan;
12 import luan.LuanTable;
13 import luan.LuanException;
14
15
16 public final class MailSender {
17 private final String host;
18 private final int port;
19 private final String username;
20 private final String password;
21
22 public MailSender(LuanTable paramsTbl) throws LuanException {
23 Map<Object,Object> params = paramsTbl.asMap();
24
25 String host = getString(params,"host");
26 if( host==null )
27 throw new LuanException( "parameter 'host' is required" );
28 this.host = host;
29
30 Object port = params.remove("port");
31 if( port==null )
32 throw new LuanException( "parameter 'port' is required" );
33 if( !(port instanceof Number) )
34 throw new LuanException( "parameter 'port' must be an integer" );
35 Integer i = Luan.asInteger(port);
36 if( i == null )
37 throw new LuanException( "parameter 'port' must be an integer" );
38 this.port = i;
39
40 this.username = getString(params,"username");
41 this.password = getString(params,"password");
42 if( this.username!=null && this.password==null )
43 throw new LuanException( "password required with username" );
44 if( this.username==null && this.password!=null )
45 throw new LuanException( "username required with password" );
46
47 if( !params.isEmpty() )
48 throw new LuanException( "unrecognized parameters: "+params );
49 }
50
51 private static String getString(Map<Object,Object> params,String key) throws LuanException {
52 Object val = params.remove(key);
53 if( val!=null && !(val instanceof String) )
54 throw new LuanException( "parameter '"+key+"' must be a string" );
55 return (String)val;
56 }
57
58 private static Message message(LuanTable mailTbl) throws LuanException {
59 Map<Object,Object> mailParams = mailTbl.asMap();
60 Object body = mailParams.remove("body");
61 if( body == null )
62 throw new LuanException( "parameter 'body' is required" );
63 Map<String,String> headers = new LinkedHashMap<String,String>();
64 boolean hasContentType = false;
65 for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
66 Object key = entry.getKey();
67 Object value = entry.getValue();
68 if( !(key instanceof String) )
69 throw new LuanException( "keys must be strings" );
70 if( !(value instanceof String) )
71 throw new LuanException( "value for '"+key+"' must be string" );
72 String name = (String)key;
73 headers.put(name,(String)value);
74 if( name.equalsIgnoreCase("content-type") )
75 hasContentType = true;
76 }
77 if( body instanceof String ) {
78 if( !hasContentType )
79 headers.put("Content-Type","text/plain; charset=utf-8");
80 return new Message(headers,(String)body);
81 }
82 if( body instanceof byte[] ) {
83 if( !hasContentType )
84 throw new LuanException("Content-Type required for binary body");
85 return new Message(headers,(byte[])body);
86 }
87 if( body instanceof LuanTable ) {
88 if( !hasContentType )
89 headers.put("Content-Type","multipart/mixed");
90 LuanTable tbl = (LuanTable)body;
91 if( !tbl.isList() )
92 throw new LuanException( "body table must be a list" );
93 List list = tbl.asList();
94 Message[] msgs = new Message[list.size()];
95 for( int i=0; i<msgs.length; i++ ) {
96 Object obj = list.get(i);
97 if( !(obj instanceof LuanTable) )
98 throw new LuanException( "body table must be a list of part tables" );
99 msgs[i] = message((LuanTable)obj);
100 }
101 return new Message(headers,msgs);
102 }
103 throw new LuanException("body must be a string, binary, or list of part tables");
104 }
105
106 public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {
107 Message msg = message(mailTbl);
108 Socket socket = new Socket(host,port);
109 Smtp smtp = new Smtp(socket);
110 if( username != null )
111 smtp.authenticate(username,password);
112 smtp.send(msg);
113 smtp.close();
114 }
115
116 }