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