changeset 270:b2c20fdcf42a

allow alternative-multipart mail git-svn-id: https://luan-java.googlecode.com/svn/trunk@271 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 30 Oct 2014 20:29:33 +0000
parents 967fc3d76a9f
children 82a3ebcfbafa
files dist/jars/luan-core-trunk.jar dist/jars/luan-logging-trunk.jar dist/jars/luan-lucene-trunk.jar dist/jars/luan-mail-trunk.jar dist/jars/luan-web-trunk.jar mail/src/luan/modules/mail/SmtpCon.java
diffstat 6 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
diff -r 967fc3d76a9f -r b2c20fdcf42a dist/jars/luan-core-trunk.jar
Binary file dist/jars/luan-core-trunk.jar has changed
diff -r 967fc3d76a9f -r b2c20fdcf42a dist/jars/luan-logging-trunk.jar
Binary file dist/jars/luan-logging-trunk.jar has changed
diff -r 967fc3d76a9f -r b2c20fdcf42a dist/jars/luan-lucene-trunk.jar
Binary file dist/jars/luan-lucene-trunk.jar has changed
diff -r 967fc3d76a9f -r b2c20fdcf42a dist/jars/luan-mail-trunk.jar
Binary file dist/jars/luan-mail-trunk.jar has changed
diff -r 967fc3d76a9f -r b2c20fdcf42a dist/jars/luan-web-trunk.jar
Binary file dist/jars/luan-web-trunk.jar has changed
diff -r 967fc3d76a9f -r b2c20fdcf42a mail/src/luan/modules/mail/SmtpCon.java
--- a/mail/src/luan/modules/mail/SmtpCon.java	Wed Oct 29 23:05:59 2014 +0000
+++ b/mail/src/luan/modules/mail/SmtpCon.java	Thu Oct 30 20:29:33 2014 +0000
@@ -1,5 +1,6 @@
 package luan.modules.mail;
 
+import java.util.Map;
 import java.util.Properties;
 import javax.mail.Authenticator;
 import javax.mail.PasswordAuthentication;
@@ -8,6 +9,8 @@
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimeBodyPart;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -64,7 +67,7 @@
 	private String getString(LuanState luan,LuanTable params,String key) throws LuanException {
 		Object val = params.get(key);
 		if( val!=null && !(val instanceof String) )
-			throw luan.exception( "parameter '"+key+"' is must be a string" );
+			throw luan.exception( "parameter '"+key+"' must be a string" );
 		return (String)val;
 	}
 
@@ -97,9 +100,28 @@
 			if( subject != null )
 				msg.setSubject(subject);
 
-			String body = getString(luan,mailTbl,"body");
-			if( body != null )
-				msg.setText(body);
+			Object body = mailTbl.get("body");
+			if( body != null ) {
+				if( body instanceof String ) {
+					msg.setText((String)body);
+				} else if( body instanceof LuanTable ) {
+					MimeMultipart mp = new MimeMultipart("alternative");
+					for( Map.Entry<Object,Object> entry : (LuanTable)body ) {
+						String key = (String)entry.getKey();
+						String val = (String)entry.getValue();
+						MimeBodyPart part = new MimeBodyPart();
+						if( key.equals("text") ) {
+							part.setText(val);
+						} else if( key.equals("html") ) {
+							part.setContent(val,"text/html");
+						} else
+							throw luan.exception( "invalid body type: " + key );
+						mp.addBodyPart(part);
+					}
+					msg.setContent(mp);
+				} else
+					throw luan.exception( "parameter 'body' is must be a string or table" );
+			}
 
 			Transport.send(msg);
 		} catch(MessagingException e) {