]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java
Clarify SLC project structure.
[gpl/argeo-slc.git] / cms / org.argeo.slc.support / src / 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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.slc.SlcException;
16 import org.argeo.slc.core.execution.tasks.SystemCall;
17
18 /** Sends a mail via JavaMail, local mail command or Google Mail. */
19 public class SendMail implements Runnable {
20 // See:
21 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
22 // http://java.sun.com/products/javamail/FAQ.html#gmail
23
24 private final static Log log = LogFactory.getLog(SendMail.class);
25
26 private String host;
27 private String from;
28 private String to;
29 private String subject;
30 private String text;
31 private String username;
32 private String password;
33 private Map<String, String> javaMailProperties = new HashMap<String, String>();
34
35 public void run() {
36 if ("local".equals(host))
37 sendWithMailCommand();
38 else if ("smtp.gmail.com".equals(host))
39 sendWithGMail();
40 else
41 sendWithJavaMail();
42 }
43
44 protected void sendWithMailCommand() {
45 SystemCall mail = new SystemCall("mail");
46 mail.arg("-s", subject).arg(to);
47 mail.run();
48 if (log.isDebugEnabled())
49 log.debug("Sent mail to " + to + " with OS mail command");
50 }
51
52 protected void sendWithJavaMail() {
53 try {
54 // Get system properties
55 Properties props = System.getProperties();
56
57 // Setup mail server
58 props.put("mail.smtp.host", host);
59
60 for (String key : javaMailProperties.keySet())
61 props.put(key, javaMailProperties.get(key));
62
63 // Get session
64 Session session = Session.getDefaultInstance(props, null);
65
66 // Define message
67 MimeMessage message = new MimeMessage(session);
68 buildJavaMailMessage(message);
69
70 // Send message
71 Transport.send(message);
72 if (log.isDebugEnabled())
73 log.debug("Sent mail to " + to + " with JavaMail");
74 } catch (Exception e) {
75 throw new SlcException("Cannot send message.", e);
76 }
77 }
78
79 protected void sendWithGMail() {
80 try {
81 Properties props = new Properties();
82 props.put("mail.smtps.auth", "true");
83 props.put("mail.smtps.host", host);
84 Session session = Session.getDefaultInstance(props, null);
85 MimeMessage message = new MimeMessage(session);
86 buildJavaMailMessage(message);
87 Transport t = session.getTransport("smtps");
88 try {
89 t.connect(host, username, password);
90 t.sendMessage(message, message.getAllRecipients());
91 } finally {
92 t.close();
93 }
94 if (log.isDebugEnabled())
95 log.debug("Sent mail to " + to + " with Google Mail");
96 } catch (Exception e) {
97 throw new SlcException("Cannot send message.", e);
98 }
99 }
100
101 protected void buildJavaMailMessage(Message message) throws Exception {
102 message.setFrom(new InternetAddress(from));
103 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
104 message.setSubject(subject);
105 message.setText(text);
106 }
107
108 public void setHost(String host) {
109 this.host = host;
110 }
111
112 public void setFrom(String from) {
113 this.from = from;
114 }
115
116 public void setTo(String to) {
117 this.to = to;
118 }
119
120 public void setSubject(String subject) {
121 this.subject = subject;
122 }
123
124 public void setText(String text) {
125 this.text = text;
126 }
127
128 public void setJavaMailProperties(Map<String, String> javaMailProperties) {
129 this.javaMailProperties = javaMailProperties;
130 }
131
132 public void setUsername(String username) {
133 this.username = username;
134 }
135
136 public void setPassword(String password) {
137 this.password = password;
138 }
139
140 }