Mercurial Hosting > luan
comparison mail/src/luan/modules/mail/SmtpCon.java @ 228:7580379cdc79
implement basic mail smtp
git-svn-id: https://luan-java.googlecode.com/svn/trunk@229 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 24 Sep 2014 03:39:34 +0000 |
parents | |
children | b2c20fdcf42a |
comparison
equal
deleted
inserted
replaced
227:c0f87c1ba99f | 228:7580379cdc79 |
---|---|
1 package luan.modules.mail; | |
2 | |
3 import java.util.Properties; | |
4 import javax.mail.Authenticator; | |
5 import javax.mail.PasswordAuthentication; | |
6 import javax.mail.Session; | |
7 import javax.mail.Transport; | |
8 import javax.mail.Message; | |
9 import javax.mail.MessagingException; | |
10 import javax.mail.internet.MimeMessage; | |
11 import luan.Luan; | |
12 import luan.LuanState; | |
13 import luan.LuanTable; | |
14 import luan.LuanJavaFunction; | |
15 import luan.LuanException; | |
16 | |
17 | |
18 public final class SmtpCon { | |
19 private final Session session; | |
20 | |
21 public SmtpCon(LuanState luan,LuanTable params) throws LuanException { | |
22 Properties props = new Properties(System.getProperties()); | |
23 | |
24 String host = getString(luan,params,"host"); | |
25 if( host==null ) | |
26 throw luan.exception( "parameter 'host' is required" ); | |
27 props.setProperty("mail.smtp.host",host); | |
28 | |
29 Object port = params.get("port"); | |
30 if( port != null ) { | |
31 String s; | |
32 if( port instanceof String ) { | |
33 s = (String)port; | |
34 } else if( port instanceof Number ) { | |
35 Integer i = Luan.asInteger(port); | |
36 if( i == null ) | |
37 throw luan.exception( "parameter 'port' is must be an integer" ); | |
38 s = i.toString(); | |
39 } else { | |
40 throw luan.exception( "parameter 'port' is must be an integer" ); | |
41 } | |
42 props.setProperty("mail.smtp.socketFactory.port", s); | |
43 props.setProperty("mail.smtp.port", s); | |
44 } | |
45 | |
46 String username = getString(luan,params,"username"); | |
47 if( username == null ) { | |
48 session = Session.getInstance(props); | |
49 } else { | |
50 String password = getString(luan,params,"password"); | |
51 if( password==null ) | |
52 throw luan.exception( "parameter 'password' is required with 'username'" ); | |
53 props.setProperty("mail.smtp.auth","true"); | |
54 final PasswordAuthentication pa = new PasswordAuthentication(username,password); | |
55 Authenticator auth = new Authenticator() { | |
56 protected PasswordAuthentication getPasswordAuthentication() { | |
57 return pa; | |
58 } | |
59 }; | |
60 session = Session.getInstance(props,auth); | |
61 } | |
62 } | |
63 | |
64 private String getString(LuanState luan,LuanTable params,String key) throws LuanException { | |
65 Object val = params.get(key); | |
66 if( val!=null && !(val instanceof String) ) | |
67 throw luan.exception( "parameter '"+key+"' is must be a string" ); | |
68 return (String)val; | |
69 } | |
70 | |
71 public LuanTable table() { | |
72 LuanTable tbl = Luan.newTable(); | |
73 try { | |
74 tbl.put( "send", new LuanJavaFunction( | |
75 SmtpCon.class.getMethod( "send", LuanState.class, LuanTable.class ), this | |
76 ) ); | |
77 } catch(NoSuchMethodException e) { | |
78 throw new RuntimeException(e); | |
79 } | |
80 return tbl; | |
81 } | |
82 | |
83 | |
84 public void send(LuanState luan,LuanTable mailTbl) throws LuanException { | |
85 try { | |
86 MimeMessage msg = new MimeMessage(session); | |
87 | |
88 String from = getString(luan,mailTbl,"from"); | |
89 if( from != null ) | |
90 msg.setFrom(from); | |
91 | |
92 String to = getString(luan,mailTbl,"to"); | |
93 if( to != null ) | |
94 msg.setRecipients(Message.RecipientType.TO,to); | |
95 | |
96 String subject = getString(luan,mailTbl,"subject"); | |
97 if( subject != null ) | |
98 msg.setSubject(subject); | |
99 | |
100 String body = getString(luan,mailTbl,"body"); | |
101 if( body != null ) | |
102 msg.setText(body); | |
103 | |
104 Transport.send(msg); | |
105 } catch(MessagingException e) { | |
106 throw luan.exception(e); | |
107 } | |
108 } | |
109 | |
110 } |