Mercurial Hosting > luan
comparison mail/src/luan/modules/mail/SmtpCon.java @ 360:cbb94a7c7a9e
allow mail attachments;
add String.to_binary and Binary.to_string;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 15 Apr 2015 11:32:30 -0600 |
parents | ae7ae2755b48 |
children | e2e70d27c258 |
comparison
equal
deleted
inserted
replaced
359:8848edb0e6bf | 360:cbb94a7c7a9e |
---|---|
1 package luan.modules.mail; | 1 package luan.modules.mail; |
2 | 2 |
3 import java.io.IOException; | |
3 import java.util.Map; | 4 import java.util.Map; |
4 import java.util.HashMap; | 5 import java.util.HashMap; |
5 import java.util.Properties; | 6 import java.util.Properties; |
6 import javax.mail.Authenticator; | 7 import javax.mail.Authenticator; |
7 import javax.mail.PasswordAuthentication; | 8 import javax.mail.PasswordAuthentication; |
8 import javax.mail.Session; | 9 import javax.mail.Session; |
9 import javax.mail.Transport; | 10 import javax.mail.Transport; |
10 import javax.mail.Message; | 11 import javax.mail.Message; |
11 import javax.mail.MessagingException; | 12 import javax.mail.MessagingException; |
13 import javax.mail.Part; | |
12 import javax.mail.internet.MimeMessage; | 14 import javax.mail.internet.MimeMessage; |
13 import javax.mail.internet.MimeMultipart; | 15 import javax.mail.internet.MimeMultipart; |
14 import javax.mail.internet.MimeBodyPart; | 16 import javax.mail.internet.MimeBodyPart; |
15 import luan.Luan; | 17 import luan.Luan; |
16 import luan.LuanState; | 18 import luan.LuanState; |
37 if( port instanceof String ) { | 39 if( port instanceof String ) { |
38 s = (String)port; | 40 s = (String)port; |
39 } else if( port instanceof Number ) { | 41 } else if( port instanceof Number ) { |
40 Integer i = Luan.asInteger(port); | 42 Integer i = Luan.asInteger(port); |
41 if( i == null ) | 43 if( i == null ) |
42 throw luan.exception( "parameter 'port' is must be an integer" ); | 44 throw luan.exception( "parameter 'port' must be an integer" ); |
43 s = i.toString(); | 45 s = i.toString(); |
44 } else { | 46 } else { |
45 throw luan.exception( "parameter 'port' is must be an integer" ); | 47 throw luan.exception( "parameter 'port' must be an integer" ); |
46 } | 48 } |
47 props.setProperty("mail.smtp.socketFactory.port", s); | 49 props.setProperty("mail.smtp.socketFactory.port", s); |
48 props.setProperty("mail.smtp.port", s); | 50 props.setProperty("mail.smtp.port", s); |
49 } | 51 } |
50 | 52 |
109 String subject = getString(luan,mailParams,"subject"); | 111 String subject = getString(luan,mailParams,"subject"); |
110 if( subject != null ) | 112 if( subject != null ) |
111 msg.setSubject(subject); | 113 msg.setSubject(subject); |
112 | 114 |
113 Object body = mailParams.remove("body"); | 115 Object body = mailParams.remove("body"); |
116 Object attachments = mailParams.remove("attachments"); | |
117 Part bodyPart = attachments==null ? msg : new MimeBodyPart(); | |
118 | |
114 if( body != null ) { | 119 if( body != null ) { |
115 if( body instanceof String ) { | 120 if( body instanceof String ) { |
116 msg.setText((String)body); | 121 bodyPart.setText((String)body); |
117 } else if( body instanceof LuanTable ) { | 122 } else if( body instanceof LuanTable ) { |
118 LuanTable bodyTbl = (LuanTable)body; | 123 LuanTable bodyTbl = (LuanTable)body; |
119 Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap()); | 124 Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap()); |
120 MimeMultipart mp = new MimeMultipart("alternative"); | 125 MimeMultipart mp = new MimeMultipart("alternative"); |
121 String text = (String)map.remove("text"); | 126 String text = (String)map.remove("text"); |
130 part.setContent(html,"text/html"); | 135 part.setContent(html,"text/html"); |
131 mp.addBodyPart(part); | 136 mp.addBodyPart(part); |
132 } | 137 } |
133 if( !map.isEmpty() ) | 138 if( !map.isEmpty() ) |
134 throw luan.exception( "invalid body types: " + map ); | 139 throw luan.exception( "invalid body types: " + map ); |
135 msg.setContent(mp); | 140 bodyPart.setContent(mp); |
136 } else | 141 } else |
137 throw luan.exception( "parameter 'body' is must be a string or table" ); | 142 throw luan.exception( "parameter 'body' must be a string or table" ); |
143 } | |
144 | |
145 if( attachments != null ) { | |
146 if( !(attachments instanceof LuanTable) ) | |
147 throw luan.exception( "parameter 'attachments' must be a table" ); | |
148 LuanTable attachmentsTbl = (LuanTable)attachments; | |
149 if( !attachmentsTbl.isList() ) | |
150 throw luan.exception( "parameter 'attachments' must be a list" ); | |
151 MimeMultipart mp = new MimeMultipart("mixed"); | |
152 if( body != null ) | |
153 mp.addBodyPart((MimeBodyPart)bodyPart); | |
154 for( Object attachment : attachmentsTbl.asList() ) { | |
155 if( !(attachment instanceof LuanTable) ) | |
156 throw luan.exception( "each attachment must be a table" ); | |
157 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap()); | |
158 Object obj; | |
159 | |
160 obj = attachmentMap.remove("filename"); | |
161 if( obj==null ) | |
162 throw luan.exception( "an attachment is missing 'filename'" ); | |
163 if( !(obj instanceof String) ) | |
164 throw luan.exception( "an attachment filename must be a string" ); | |
165 String filename = (String)obj; | |
166 | |
167 obj = attachmentMap.remove("content_type"); | |
168 if( obj==null ) | |
169 throw luan.exception( "an attachment is missing 'content_type'" ); | |
170 if( !(obj instanceof String) ) | |
171 throw luan.exception( "an attachment content_type must be a string" ); | |
172 String content_type = (String)obj; | |
173 | |
174 Object content = attachmentMap.remove("content"); | |
175 if( content==null ) | |
176 throw luan.exception( "an attachment is missing 'content'" ); | |
177 if( content_type.startsWith("text/") && content instanceof byte[] ) | |
178 content = new String((byte[])content); | |
179 | |
180 if( !attachmentMap.isEmpty() ) | |
181 throw luan.exception( "unrecognized attachment parameters: "+attachmentMap ); | |
182 | |
183 MimeBodyPart part = new MimeBodyPart(); | |
184 part.setContent(content,content_type); | |
185 part.setFileName(filename); | |
186 mp.addBodyPart(part); | |
187 } | |
188 msg.setContent(mp); | |
138 } | 189 } |
139 | 190 |
140 if( !mailParams.isEmpty() ) | 191 if( !mailParams.isEmpty() ) |
141 throw luan.exception( "unrecognized parameters: "+mailParams ); | 192 throw luan.exception( "unrecognized parameters: "+mailParams ); |
142 | 193 |