]>
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) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.mail
;
18 import java
.util
.HashMap
;
20 import java
.util
.Properties
;
22 import javax
.mail
.Message
;
23 import javax
.mail
.Session
;
24 import javax
.mail
.Transport
;
25 import javax
.mail
.internet
.InternetAddress
;
26 import javax
.mail
.internet
.MimeMessage
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
33 /** Sends a mail via JavaMail, local mail command or Google Mail. */
34 public class SendMail
implements Runnable
{
36 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
37 // http://java.sun.com/products/javamail/FAQ.html#gmail
39 private final static Log log
= LogFactory
.getLog(SendMail
.class);
44 private String subject
;
46 private String username
;
47 private String password
;
48 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
51 if ("local".equals(host
))
52 sendWithMailCommand();
53 else if ("smtp.gmail.com".equals(host
))
59 protected void sendWithMailCommand() {
60 SystemCall mail
= new SystemCall("mail");
61 mail
.arg("-s", subject
).arg(to
);
63 if (log
.isDebugEnabled())
64 log
.debug("Sent mail to " + to
+ " with OS mail command");
67 protected void sendWithJavaMail() {
69 // Get system properties
70 Properties props
= System
.getProperties();
73 props
.put("mail.smtp.host", host
);
75 for (String key
: javaMailProperties
.keySet())
76 props
.put(key
, javaMailProperties
.get(key
));
79 Session session
= Session
.getDefaultInstance(props
, null);
82 MimeMessage message
= new MimeMessage(session
);
83 buildJavaMailMessage(message
);
86 Transport
.send(message
);
87 if (log
.isDebugEnabled())
88 log
.debug("Sent mail to " + to
+ " with JavaMail");
89 } catch (Exception e
) {
90 throw new SlcException("Cannot send message.", e
);
94 protected void sendWithGMail() {
96 Properties props
= new Properties();
97 props
.put("mail.smtps.auth", "true");
98 props
.put("mail.smtps.host", host
);
99 Session session
= Session
.getDefaultInstance(props
, null);
100 MimeMessage message
= new MimeMessage(session
);
101 buildJavaMailMessage(message
);
102 Transport t
= session
.getTransport("smtps");
104 t
.connect(host
, username
, password
);
105 t
.sendMessage(message
, message
.getAllRecipients());
109 if (log
.isDebugEnabled())
110 log
.debug("Sent mail to " + to
+ " with Google Mail");
111 } catch (Exception e
) {
112 throw new SlcException("Cannot send message.", e
);
116 protected void buildJavaMailMessage(Message message
) throws Exception
{
117 message
.setFrom(new InternetAddress(from
));
118 message
.addRecipient(Message
.RecipientType
.TO
, new InternetAddress(to
));
119 message
.setSubject(subject
);
120 message
.setText(text
);
123 public void setHost(String host
) {
127 public void setFrom(String from
) {
131 public void setTo(String to
) {
135 public void setSubject(String subject
) {
136 this.subject
= subject
;
139 public void setText(String text
) {
143 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
144 this.javaMailProperties
= javaMailProperties
;
147 public void setUsername(String username
) {
148 this.username
= username
;
151 public void setPassword(String password
) {
152 this.password
= password
;