]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/mail/SendMail.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.mail
;
19 import java
.util
.HashMap
;
21 import java
.util
.Properties
;
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
;
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
;
34 /** Sends a mail via JavaMail, local mail command or Google Mail. */
35 public class SendMail
implements Runnable
{
37 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
38 // http://java.sun.com/products/javamail/FAQ.html#gmail
40 private final static Log log
= LogFactory
.getLog(SendMail
.class);
45 private String subject
;
47 private String username
;
48 private String password
;
49 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
52 if ("local".equals(host
))
53 sendWithMailCommand();
54 else if ("smtp.gmail.com".equals(host
))
60 protected void sendWithMailCommand() {
61 SystemCall mail
= new SystemCall("mail");
62 mail
.arg("-s", subject
).arg(to
);
64 if (log
.isDebugEnabled())
65 log
.debug("Sent mail to " + to
+ " with OS mail command");
68 protected void sendWithJavaMail() {
70 // Get system properties
71 Properties props
= System
.getProperties();
74 props
.put("mail.smtp.host", host
);
76 for (String key
: javaMailProperties
.keySet())
77 props
.put(key
, javaMailProperties
.get(key
));
80 Session session
= Session
.getDefaultInstance(props
, null);
83 MimeMessage message
= new MimeMessage(session
);
84 buildJavaMailMessage(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
);
95 protected void sendWithGMail() {
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");
105 t
.connect(host
, username
, password
);
106 t
.sendMessage(message
, message
.getAllRecipients());
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
);
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
);
124 public void setHost(String host
) {
128 public void setFrom(String from
) {
132 public void setTo(String to
) {
136 public void setSubject(String subject
) {
137 this.subject
= subject
;
140 public void setText(String text
) {
144 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
145 this.javaMailProperties
= javaMailProperties
;
148 public void setUsername(String username
) {
149 this.username
= username
;
152 public void setPassword(String password
) {
153 this.password
= password
;