Mercurial Hosting > luan
comparison mail/src/luan/modules/mail/SmtpCon.java @ 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 | 7580379cdc79 |
children | 073044e3ac03 |
comparison
equal
deleted
inserted
replaced
269:967fc3d76a9f | 270:b2c20fdcf42a |
---|---|
1 package luan.modules.mail; | 1 package luan.modules.mail; |
2 | 2 |
3 import java.util.Map; | |
3 import java.util.Properties; | 4 import java.util.Properties; |
4 import javax.mail.Authenticator; | 5 import javax.mail.Authenticator; |
5 import javax.mail.PasswordAuthentication; | 6 import javax.mail.PasswordAuthentication; |
6 import javax.mail.Session; | 7 import javax.mail.Session; |
7 import javax.mail.Transport; | 8 import javax.mail.Transport; |
8 import javax.mail.Message; | 9 import javax.mail.Message; |
9 import javax.mail.MessagingException; | 10 import javax.mail.MessagingException; |
10 import javax.mail.internet.MimeMessage; | 11 import javax.mail.internet.MimeMessage; |
12 import javax.mail.internet.MimeMultipart; | |
13 import javax.mail.internet.MimeBodyPart; | |
11 import luan.Luan; | 14 import luan.Luan; |
12 import luan.LuanState; | 15 import luan.LuanState; |
13 import luan.LuanTable; | 16 import luan.LuanTable; |
14 import luan.LuanJavaFunction; | 17 import luan.LuanJavaFunction; |
15 import luan.LuanException; | 18 import luan.LuanException; |
62 } | 65 } |
63 | 66 |
64 private String getString(LuanState luan,LuanTable params,String key) throws LuanException { | 67 private String getString(LuanState luan,LuanTable params,String key) throws LuanException { |
65 Object val = params.get(key); | 68 Object val = params.get(key); |
66 if( val!=null && !(val instanceof String) ) | 69 if( val!=null && !(val instanceof String) ) |
67 throw luan.exception( "parameter '"+key+"' is must be a string" ); | 70 throw luan.exception( "parameter '"+key+"' must be a string" ); |
68 return (String)val; | 71 return (String)val; |
69 } | 72 } |
70 | 73 |
71 public LuanTable table() { | 74 public LuanTable table() { |
72 LuanTable tbl = Luan.newTable(); | 75 LuanTable tbl = Luan.newTable(); |
95 | 98 |
96 String subject = getString(luan,mailTbl,"subject"); | 99 String subject = getString(luan,mailTbl,"subject"); |
97 if( subject != null ) | 100 if( subject != null ) |
98 msg.setSubject(subject); | 101 msg.setSubject(subject); |
99 | 102 |
100 String body = getString(luan,mailTbl,"body"); | 103 Object body = mailTbl.get("body"); |
101 if( body != null ) | 104 if( body != null ) { |
102 msg.setText(body); | 105 if( body instanceof String ) { |
106 msg.setText((String)body); | |
107 } else if( body instanceof LuanTable ) { | |
108 MimeMultipart mp = new MimeMultipart("alternative"); | |
109 for( Map.Entry<Object,Object> entry : (LuanTable)body ) { | |
110 String key = (String)entry.getKey(); | |
111 String val = (String)entry.getValue(); | |
112 MimeBodyPart part = new MimeBodyPart(); | |
113 if( key.equals("text") ) { | |
114 part.setText(val); | |
115 } else if( key.equals("html") ) { | |
116 part.setContent(val,"text/html"); | |
117 } else | |
118 throw luan.exception( "invalid body type: " + key ); | |
119 mp.addBodyPart(part); | |
120 } | |
121 msg.setContent(mp); | |
122 } else | |
123 throw luan.exception( "parameter 'body' is must be a string or table" ); | |
124 } | |
103 | 125 |
104 Transport.send(msg); | 126 Transport.send(msg); |
105 } catch(MessagingException e) { | 127 } catch(MessagingException e) { |
106 throw luan.exception(e); | 128 throw luan.exception(e); |
107 } | 129 } |