]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/mail/SendMail.java
Restructure SLC development environment
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / mail / SendMail.java
1 package org.argeo.slc.mail;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Properties;
6
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;
12
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.core.execution.tasks.SystemCall;
15
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 {
19 private String host;
20 private String from;
21 private String to;
22 private String subject;
23 private String text;
24 private String username;
25 private String password;
26 private Map<String, String> javaMailProperties = new HashMap<String, String>();
27
28 public void run() {
29 if ("local".equals(host))
30 sendWithMailCommand();
31 else if ("smtp.gmail.com".equals(host))
32 sendWithGMail();
33 else
34 sendWithJavaMail();
35 }
36
37 protected void sendWithMailCommand() {
38 SystemCall mail = new SystemCall("mail");
39 mail.arg("-s", subject).arg(to);
40 mail.run();
41 }
42
43 protected void sendWithJavaMail() {
44 try {
45 // Get system properties
46 Properties props = System.getProperties();
47
48 // Setup mail server
49 props.put("mail.smtp.host", host);
50
51 for (String key : javaMailProperties.keySet())
52 props.put(key, javaMailProperties.get(key));
53
54 // Get session
55 Session session = Session.getDefaultInstance(props, null);
56
57 // Define message
58 MimeMessage message = new MimeMessage(session);
59 buildJavaMailMessage(message);
60
61 // Send message
62 Transport.send(message);
63 } catch (Exception e) {
64 throw new SlcException("Cannot send message.", e);
65 }
66 }
67
68 protected void sendWithGMail() {
69 try {
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");
77 try {
78 t.connect(host, username, password);
79 t.sendMessage(message, message.getAllRecipients());
80 } finally {
81 t.close();
82 }
83
84 } catch (Exception e) {
85 throw new SlcException("Cannot send message.", e);
86 }
87 }
88
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);
94 }
95
96 public void setHost(String host) {
97 this.host = host;
98 }
99
100 public void setFrom(String from) {
101 this.from = from;
102 }
103
104 public void setTo(String to) {
105 this.to = to;
106 }
107
108 public void setSubject(String subject) {
109 this.subject = subject;
110 }
111
112 public void setText(String text) {
113 this.text = text;
114 }
115
116 public void setJavaMailProperties(Map<String, String> javaMailProperties) {
117 this.javaMailProperties = javaMailProperties;
118 }
119
120 public void setUsername(String username) {
121 this.username = username;
122 }
123
124 public void setPassword(String password) {
125 this.password = password;
126 }
127
128 }