comparison src/luan/modules/mail/MailCon.java @ 1587:fa1a9aceac3e

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 23:22:53 -0700
parents fcca0ddf5a4d
children
comparison
equal deleted inserted replaced
1586:fcca0ddf5a4d 1587:fa1a9aceac3e
58 private static Message message(LuanTable mailTbl) throws LuanException { 58 private static Message message(LuanTable mailTbl) throws LuanException {
59 Map<Object,Object> mailParams = mailTbl.asMap(); 59 Map<Object,Object> mailParams = mailTbl.asMap();
60 Object body = mailParams.remove("body"); 60 Object body = mailParams.remove("body");
61 if( body == null ) 61 if( body == null )
62 throw new LuanException( "parameter 'body' is required" ); 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>(); 63 Map<String,String> headers = new LinkedHashMap<String,String>();
78 boolean hasContentType = false; 64 boolean hasContentType = false;
79 for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) { 65 for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
80 Object key = entry.getKey(); 66 Object key = entry.getKey();
81 Object value = entry.getValue(); 67 Object value = entry.getValue();
86 String name = (String)key; 72 String name = (String)key;
87 headers.put(name,(String)value); 73 headers.put(name,(String)value);
88 if( name.equalsIgnoreCase("content-type") ) 74 if( name.equalsIgnoreCase("content-type") )
89 hasContentType = true; 75 hasContentType = true;
90 } 76 }
91 if( !hasContentType && body instanceof String ) 77 if( body instanceof String ) {
92 headers.put("Content-Type","text/plain; charset=utf-8"); 78 if( !hasContentType )
93 return new Message(headers,body); 79 headers.put("Content-Type","text/plain; charset=utf-8");
80 return new Message(headers,(String)body);
81 }
82 if( body instanceof byte[] ) {
83 return new Message(headers,(byte[])body);
84 }
85 if( body instanceof LuanTable ) {
86 LuanTable tbl = (LuanTable)body;
87 if( !tbl.isList() )
88 throw new LuanException( "body table must be a list" );
89 List list = tbl.asList();
90 Message[] msgs = new Message[list.size()];
91 for( int i=0; i<msgs.length; i++ ) {
92 Object obj = list.get(i);
93 if( !(obj instanceof LuanTable) )
94 throw new LuanException( "body table must be a list of part tables" );
95 msgs[i] = message((LuanTable)obj);
96 }
97 return new Message(headers,msgs);
98 }
99 throw new LuanException("body must be a string, binary, or list of part tables");
94 } 100 }
95 101
96 public void send(LuanTable mailTbl) throws LuanException, IOException, MailException { 102 public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {
97 Message msg = message(mailTbl); 103 Message msg = message(mailTbl);
98 Socket socket = new Socket(host,port); 104 Socket socket = new Socket(host,port);