Mercurial Hosting > luan
comparison mail/src/luan/modules/mail/SmtpCon.java @ 289:ae7ae2755b48
improve Mail;
add String.concat();
git-svn-id: https://luan-java.googlecode.com/svn/trunk@290 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 08 Dec 2014 07:45:40 +0000 |
parents | 073044e3ac03 |
children | cbb94a7c7a9e |
comparison
equal
deleted
inserted
replaced
288:c4ed33e95889 | 289:ae7ae2755b48 |
---|---|
20 | 20 |
21 | 21 |
22 public final class SmtpCon { | 22 public final class SmtpCon { |
23 private final Session session; | 23 private final Session session; |
24 | 24 |
25 public SmtpCon(LuanState luan,LuanTable params) throws LuanException { | 25 public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException { |
26 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap()); | |
26 Properties props = new Properties(System.getProperties()); | 27 Properties props = new Properties(System.getProperties()); |
27 | 28 |
28 String host = getString(luan,params,"host"); | 29 String host = getString(luan,params,"host"); |
29 if( host==null ) | 30 if( host==null ) |
30 throw luan.exception( "parameter 'host' is required" ); | 31 throw luan.exception( "parameter 'host' is required" ); |
31 props.setProperty("mail.smtp.host",host); | 32 props.setProperty("mail.smtp.host",host); |
32 | 33 |
33 Object port = params.get("port"); | 34 Object port = params.remove("port"); |
34 if( port != null ) { | 35 if( port != null ) { |
35 String s; | 36 String s; |
36 if( port instanceof String ) { | 37 if( port instanceof String ) { |
37 s = (String)port; | 38 s = (String)port; |
38 } else if( port instanceof Number ) { | 39 } else if( port instanceof Number ) { |
61 return pa; | 62 return pa; |
62 } | 63 } |
63 }; | 64 }; |
64 session = Session.getInstance(props,auth); | 65 session = Session.getInstance(props,auth); |
65 } | 66 } |
67 | |
68 if( !params.isEmpty() ) | |
69 throw luan.exception( "unrecognized parameters: "+params ); | |
66 } | 70 } |
67 | 71 |
68 private String getString(LuanState luan,LuanTable params,String key) throws LuanException { | 72 private String getString(LuanState luan,Map<Object,Object> params,String key) throws LuanException { |
69 Object val = params.get(key); | 73 Object val = params.remove(key); |
70 if( val!=null && !(val instanceof String) ) | 74 if( val!=null && !(val instanceof String) ) |
71 throw luan.exception( "parameter '"+key+"' must be a string" ); | 75 throw luan.exception( "parameter '"+key+"' must be a string" ); |
72 return (String)val; | 76 return (String)val; |
73 } | 77 } |
74 | 78 |
85 } | 89 } |
86 | 90 |
87 | 91 |
88 public void send(LuanState luan,LuanTable mailTbl) throws LuanException { | 92 public void send(LuanState luan,LuanTable mailTbl) throws LuanException { |
89 try { | 93 try { |
94 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap()); | |
90 MimeMessage msg = new MimeMessage(session); | 95 MimeMessage msg = new MimeMessage(session); |
91 | 96 |
92 String from = getString(luan,mailTbl,"from"); | 97 String from = getString(luan,mailParams,"from"); |
93 if( from != null ) | 98 if( from != null ) |
94 msg.setFrom(from); | 99 msg.setFrom(from); |
95 | 100 |
96 String to = getString(luan,mailTbl,"to"); | 101 String to = getString(luan,mailParams,"to"); |
97 if( to != null ) | 102 if( to != null ) |
98 msg.setRecipients(Message.RecipientType.TO,to); | 103 msg.setRecipients(Message.RecipientType.TO,to); |
99 | 104 |
100 String subject = getString(luan,mailTbl,"subject"); | 105 String cc = getString(luan,mailParams,"cc"); |
106 if( cc != null ) | |
107 msg.setRecipients(Message.RecipientType.CC,cc); | |
108 | |
109 String subject = getString(luan,mailParams,"subject"); | |
101 if( subject != null ) | 110 if( subject != null ) |
102 msg.setSubject(subject); | 111 msg.setSubject(subject); |
103 | 112 |
104 Object body = mailTbl.get("body"); | 113 Object body = mailParams.remove("body"); |
105 if( body != null ) { | 114 if( body != null ) { |
106 if( body instanceof String ) { | 115 if( body instanceof String ) { |
107 msg.setText((String)body); | 116 msg.setText((String)body); |
108 } else if( body instanceof LuanTable ) { | 117 } else if( body instanceof LuanTable ) { |
109 LuanTable bodyTbl = (LuanTable)body; | 118 LuanTable bodyTbl = (LuanTable)body; |
126 msg.setContent(mp); | 135 msg.setContent(mp); |
127 } else | 136 } else |
128 throw luan.exception( "parameter 'body' is must be a string or table" ); | 137 throw luan.exception( "parameter 'body' is must be a string or table" ); |
129 } | 138 } |
130 | 139 |
140 if( !mailParams.isEmpty() ) | |
141 throw luan.exception( "unrecognized parameters: "+mailParams ); | |
142 | |
131 Transport.send(msg); | 143 Transport.send(msg); |
132 } catch(MessagingException e) { | 144 } catch(MessagingException e) { |
133 throw luan.exception(e); | 145 throw luan.exception(e); |
134 } | 146 } |
135 } | 147 } |