]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/mail/SendMail.java
04e1b503f9a6afae676e2796a853a46714d73d91
1 package org
.argeo
.slc
.mail
;
3 import java
.util
.HashMap
;
5 import java
.util
.Properties
;
7 import javax
.mail
.Message
;
8 import javax
.mail
.Session
;
9 import javax
.mail
.Transport
;
10 import javax
.mail
.internet
.InternetAddress
;
11 import javax
.mail
.internet
.MimeMessage
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
16 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
17 // http://java.sun.com/products/javamail/FAQ.html#gmail
18 public class SendMail
implements Runnable
{
22 private String subject
;
24 private String username
;
25 private String password
;
26 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
29 if ("local".equals(host
))
30 sendWithMailCommand();
31 else if ("smtp.gmail.com".equals(host
))
37 protected void sendWithMailCommand() {
38 SystemCall mail
= new SystemCall("mail");
39 mail
.arg("-s", subject
).arg(to
);
43 protected void sendWithJavaMail() {
45 // Get system properties
46 Properties props
= System
.getProperties();
49 props
.put("mail.smtp.host", host
);
51 for (String key
: javaMailProperties
.keySet())
52 props
.put(key
, javaMailProperties
.get(key
));
55 Session session
= Session
.getDefaultInstance(props
, null);
58 MimeMessage message
= new MimeMessage(session
);
59 buildJavaMailMessage(message
);
62 Transport
.send(message
);
63 } catch (Exception e
) {
64 throw new SlcException("Cannot send message.", e
);
68 protected void sendWithGMail() {
70 Properties props
= new Properties();
71 props
.put("mail.smtps.auth", "true");
72 props
.put("mail.smtps.host", host
);
73 Session session
= Session
.getDefaultInstance(props
, null);
74 MimeMessage message
= new MimeMessage(session
);
75 buildJavaMailMessage(message
);
76 Transport t
= session
.getTransport("smtps");
78 t
.connect(host
, username
, password
);
79 t
.sendMessage(message
, message
.getAllRecipients());
84 } catch (Exception e
) {
85 throw new SlcException("Cannot send message.", e
);
89 protected void buildJavaMailMessage(Message message
) throws Exception
{
90 message
.setFrom(new InternetAddress(from
));
91 message
.addRecipient(Message
.RecipientType
.TO
, new InternetAddress(to
));
92 message
.setSubject(subject
);
93 message
.setText(text
);
96 public void setHost(String host
) {
100 public void setFrom(String from
) {
104 public void setTo(String to
) {
108 public void setSubject(String subject
) {
109 this.subject
= subject
;
112 public void setText(String text
) {
116 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
117 this.javaMailProperties
= javaMailProperties
;
120 public void setUsername(String username
) {
121 this.username
= username
;
124 public void setPassword(String password
) {
125 this.password
= password
;