]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/mail/SendMail.java
Move Swing JSCH UI in a separate package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / mail / SendMail.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.mail;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22
23 import javax.mail.Message;
24 import javax.mail.Session;
25 import javax.mail.Transport;
26 import javax.mail.internet.InternetAddress;
27 import javax.mail.internet.MimeMessage;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.core.execution.tasks.SystemCall;
33
34 /** Sends a mail via JavaMail, local mail command or Google Mail. */
35 public class SendMail implements Runnable {
36 // See:
37 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
38 // http://java.sun.com/products/javamail/FAQ.html#gmail
39
40 private final static Log log = LogFactory.getLog(SendMail.class);
41
42 private String host;
43 private String from;
44 private String to;
45 private String subject;
46 private String text;
47 private String username;
48 private String password;
49 private Map<String, String> javaMailProperties = new HashMap<String, String>();
50
51 public void run() {
52 if ("local".equals(host))
53 sendWithMailCommand();
54 else if ("smtp.gmail.com".equals(host))
55 sendWithGMail();
56 else
57 sendWithJavaMail();
58 }
59
60 protected void sendWithMailCommand() {
61 SystemCall mail = new SystemCall("mail");
62 mail.arg("-s", subject).arg(to);
63 mail.run();
64 if (log.isDebugEnabled())
65 log.debug("Sent mail to " + to + " with OS mail command");
66 }
67
68 protected void sendWithJavaMail() {
69 try {
70 // Get system properties
71 Properties props = System.getProperties();
72
73 // Setup mail server
74 props.put("mail.smtp.host", host);
75
76 for (String key : javaMailProperties.keySet())
77 props.put(key, javaMailProperties.get(key));
78
79 // Get session
80 Session session = Session.getDefaultInstance(props, null);
81
82 // Define message
83 MimeMessage message = new MimeMessage(session);
84 buildJavaMailMessage(message);
85
86 // Send message
87 Transport.send(message);
88 if (log.isDebugEnabled())
89 log.debug("Sent mail to " + to + " with JavaMail");
90 } catch (Exception e) {
91 throw new SlcException("Cannot send message.", e);
92 }
93 }
94
95 protected void sendWithGMail() {
96 try {
97 Properties props = new Properties();
98 props.put("mail.smtps.auth", "true");
99 props.put("mail.smtps.host", host);
100 Session session = Session.getDefaultInstance(props, null);
101 MimeMessage message = new MimeMessage(session);
102 buildJavaMailMessage(message);
103 Transport t = session.getTransport("smtps");
104 try {
105 t.connect(host, username, password);
106 t.sendMessage(message, message.getAllRecipients());
107 } finally {
108 t.close();
109 }
110 if (log.isDebugEnabled())
111 log.debug("Sent mail to " + to + " with Google Mail");
112 } catch (Exception e) {
113 throw new SlcException("Cannot send message.", e);
114 }
115 }
116
117 protected void buildJavaMailMessage(Message message) throws Exception {
118 message.setFrom(new InternetAddress(from));
119 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
120 message.setSubject(subject);
121 message.setText(text);
122 }
123
124 public void setHost(String host) {
125 this.host = host;
126 }
127
128 public void setFrom(String from) {
129 this.from = from;
130 }
131
132 public void setTo(String to) {
133 this.to = to;
134 }
135
136 public void setSubject(String subject) {
137 this.subject = subject;
138 }
139
140 public void setText(String text) {
141 this.text = text;
142 }
143
144 public void setJavaMailProperties(Map<String, String> javaMailProperties) {
145 this.javaMailProperties = javaMailProperties;
146 }
147
148 public void setUsername(String username) {
149 this.username = username;
150 }
151
152 public void setPassword(String password) {
153 this.password = password;
154 }
155
156 }