1586
|
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 MailCon {
|
|
17 private final String host;
|
|
18 private final int port;
|
|
19 private final String username;
|
|
20 private final String password;
|
|
21
|
|
22 public MailCon(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 if( body instanceof LuanTable ) {
|
|
64 LuanTable tbl = (LuanTable)body;
|
|
65 if( !tbl.isList() )
|
|
66 throw new LuanException( "body table must be a list" );
|
|
67 List list = tbl.asList();
|
|
68 Message[] msgs = new Message[list.size()];
|
|
69 for( int i=0; i<msgs.length; i++ ) {
|
|
70 Object obj = list.get(i);
|
|
71 if( !(obj instanceof LuanTable) )
|
|
72 throw new LuanException( "body table must be a list of part tables" );
|
|
73 msgs[i] = message((LuanTable)obj);
|
|
74 }
|
|
75 body = msgs;
|
|
76 }
|
|
77 Map<String,String> headers = new LinkedHashMap<String,String>();
|
|
78 boolean hasContentType = false;
|
|
79 for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
|
|
80 Object key = entry.getKey();
|
|
81 Object value = entry.getValue();
|
|
82 if( !(key instanceof String) )
|
|
83 throw new LuanException( "keys must be strings" );
|
|
84 if( !(value instanceof String) )
|
|
85 throw new LuanException( "value for '"+key+"' must be string" );
|
|
86 String name = (String)key;
|
|
87 headers.put(name,(String)value);
|
|
88 if( name.equalsIgnoreCase("content-type") )
|
|
89 hasContentType = true;
|
|
90 }
|
|
91 if( !hasContentType && body instanceof String )
|
|
92 headers.put("Content-Type","text/plain; charset=utf-8");
|
|
93 return new Message(headers,body);
|
|
94 }
|
|
95
|
|
96 public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {
|
|
97 Message msg = message(mailTbl);
|
|
98 Socket socket = new Socket(host,port);
|
|
99 Smtp smtp = new Smtp(socket);
|
|
100 if( username != null )
|
|
101 smtp.authenticate(username,password);
|
|
102 smtp.send(msg);
|
|
103 smtp.close();
|
|
104 }
|
|
105
|
|
106 }
|