]>
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
.argeo
.slc
.SlcException
;
30 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
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
{
38 private String subject
;
40 private String username
;
41 private String password
;
42 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
45 if ("local".equals(host
))
46 sendWithMailCommand();
47 else if ("smtp.gmail.com".equals(host
))
53 protected void sendWithMailCommand() {
54 SystemCall mail
= new SystemCall("mail");
55 mail
.arg("-s", subject
).arg(to
);
59 protected void sendWithJavaMail() {
61 // Get system properties
62 Properties props
= System
.getProperties();
65 props
.put("mail.smtp.host", host
);
67 for (String key
: javaMailProperties
.keySet())
68 props
.put(key
, javaMailProperties
.get(key
));
71 Session session
= Session
.getDefaultInstance(props
, null);
74 MimeMessage message
= new MimeMessage(session
);
75 buildJavaMailMessage(message
);
78 Transport
.send(message
);
79 } catch (Exception e
) {
80 throw new SlcException("Cannot send message.", e
);
84 protected void sendWithGMail() {
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");
94 t
.connect(host
, username
, password
);
95 t
.sendMessage(message
, message
.getAllRecipients());
100 } catch (Exception e
) {
101 throw new SlcException("Cannot send message.", e
);
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
);
112 public void setHost(String host
) {
116 public void setFrom(String from
) {
120 public void setTo(String to
) {
124 public void setSubject(String subject
) {
125 this.subject
= subject
;
128 public void setText(String text
) {
132 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
133 this.javaMailProperties
= javaMailProperties
;
136 public void setUsername(String username
) {
137 this.username
= username
;
140 public void setPassword(String password
) {
141 this.password
= password
;