comparison src/luan/modules/mail/SmtpCon.java @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents mail/src/luan/modules/mail/SmtpCon.java@b21d82ee5756
children 23e8f93055a4
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 package luan.modules.mail;
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.HashMap;
6 import java.util.Properties;
7 import javax.mail.Authenticator;
8 import javax.mail.PasswordAuthentication;
9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.Message;
12 import javax.mail.MessagingException;
13 import javax.mail.Part;
14 import javax.mail.internet.MimeMessage;
15 import javax.mail.internet.MimeMultipart;
16 import javax.mail.internet.MimeBodyPart;
17 import luan.Luan;
18 import luan.LuanState;
19 import luan.LuanTable;
20 import luan.LuanException;
21
22
23 public final class SmtpCon {
24 private final Session session;
25
26 public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException {
27 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap(luan));
28 Properties props = new Properties(System.getProperties());
29
30 String host = getString(params,"host");
31 if( host==null )
32 throw new LuanException( "parameter 'host' is required" );
33 props.setProperty("mail.smtp.host",host);
34
35 Object port = params.remove("port");
36 if( port != null ) {
37 String s;
38 if( port instanceof String ) {
39 s = (String)port;
40 } else if( port instanceof Number ) {
41 Integer i = Luan.asInteger(port);
42 if( i == null )
43 throw new LuanException( "parameter 'port' must be an integer" );
44 s = i.toString();
45 } else {
46 throw new LuanException( "parameter 'port' must be an integer" );
47 }
48 props.setProperty("mail.smtp.socketFactory.port", s);
49 props.setProperty("mail.smtp.port", s);
50 }
51
52 String username = getString(params,"username");
53 if( username == null ) {
54 session = Session.getInstance(props);
55 } else {
56 String password = getString(params,"password");
57 if( password==null )
58 throw new LuanException( "parameter 'password' is required with 'username'" );
59 props.setProperty("mail.smtp.auth","true");
60 final PasswordAuthentication pa = new PasswordAuthentication(username,password);
61 Authenticator auth = new Authenticator() {
62 protected PasswordAuthentication getPasswordAuthentication() {
63 return pa;
64 }
65 };
66 session = Session.getInstance(props,auth);
67 }
68
69 if( !params.isEmpty() )
70 throw new LuanException( "unrecognized parameters: "+params );
71 }
72
73 private String getString(Map<Object,Object> params,String key) throws LuanException {
74 Object val = params.remove(key);
75 if( val!=null && !(val instanceof String) )
76 throw new LuanException( "parameter '"+key+"' must be a string" );
77 return (String)val;
78 }
79
80
81 public void send(LuanState luan,LuanTable mailTbl) throws LuanException {
82 try {
83 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap(luan));
84 MimeMessage msg = new MimeMessage(session);
85
86 String from = getString(mailParams,"from");
87 if( from != null )
88 msg.setFrom(from);
89
90 String to = getString(mailParams,"to");
91 if( to != null )
92 msg.setRecipients(Message.RecipientType.TO,to);
93
94 String cc = getString(mailParams,"cc");
95 if( cc != null )
96 msg.setRecipients(Message.RecipientType.CC,cc);
97
98 String subject = getString(mailParams,"subject");
99 if( subject != null )
100 msg.setSubject(subject);
101
102 Object body = mailParams.remove("body");
103 Object attachments = mailParams.remove("attachments");
104 Part bodyPart = attachments==null ? msg : new MimeBodyPart();
105
106 if( body != null ) {
107 if( body instanceof String ) {
108 bodyPart.setText((String)body);
109 } else if( body instanceof LuanTable ) {
110 LuanTable bodyTbl = (LuanTable)body;
111 Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap(luan));
112 MimeMultipart mp = new MimeMultipart("alternative");
113 String text = (String)map.remove("text");
114 if( text != null ) {
115 MimeBodyPart part = new MimeBodyPart();
116 part.setText(text);
117 mp.addBodyPart(part);
118 }
119 String html = (String)map.remove("html");
120 if( html != null ) {
121 MimeBodyPart part = new MimeBodyPart();
122 part.setContent(html,"text/html");
123 mp.addBodyPart(part);
124 }
125 if( !map.isEmpty() )
126 throw new LuanException( "invalid body types: " + map );
127 bodyPart.setContent(mp);
128 } else
129 throw new LuanException( "parameter 'body' must be a string or table" );
130 }
131
132 if( attachments != null ) {
133 if( !(attachments instanceof LuanTable) )
134 throw new LuanException( "parameter 'attachments' must be a table" );
135 LuanTable attachmentsTbl = (LuanTable)attachments;
136 if( !attachmentsTbl.isList() )
137 throw new LuanException( "parameter 'attachments' must be a list" );
138 MimeMultipart mp = new MimeMultipart("mixed");
139 if( body != null )
140 mp.addBodyPart((MimeBodyPart)bodyPart);
141 for( Object attachment : attachmentsTbl.asList() ) {
142 if( !(attachment instanceof LuanTable) )
143 throw new LuanException( "each attachment must be a table" );
144 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap(luan));
145 Object obj;
146
147 obj = attachmentMap.remove("filename");
148 if( obj==null )
149 throw new LuanException( "an attachment is missing 'filename'" );
150 if( !(obj instanceof String) )
151 throw new LuanException( "an attachment filename must be a string" );
152 String filename = (String)obj;
153
154 obj = attachmentMap.remove("content_type");
155 if( obj==null )
156 throw new LuanException( "an attachment is missing 'content_type'" );
157 if( !(obj instanceof String) )
158 throw new LuanException( "an attachment content_type must be a string" );
159 String content_type = (String)obj;
160
161 Object content = attachmentMap.remove("content");
162 if( content==null )
163 throw new LuanException( "an attachment is missing 'content'" );
164 if( content_type.startsWith("text/") && content instanceof byte[] )
165 content = new String((byte[])content);
166
167 if( !attachmentMap.isEmpty() )
168 throw new LuanException( "unrecognized attachment parameters: "+attachmentMap );
169
170 MimeBodyPart part = new MimeBodyPart();
171 part.setContent(content,content_type);
172 part.setFileName(filename);
173 mp.addBodyPart(part);
174 }
175 msg.setContent(mp);
176 }
177
178 if( !mailParams.isEmpty() )
179 throw new LuanException( "unrecognized parameters: "+mailParams );
180
181 Transport.send(msg);
182 } catch(MessagingException e) {
183 throw new LuanException(e);
184 }
185 }
186
187 }