]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java
1 package org
.argeo
.slc
.mail
;
3 import java
.util
.HashMap
;
5 import java
.util
.Properties
;
7 import javax
.mail
.Message
;
8 import javax
.mail
.Session
;
9 import javax
.mail
.Transport
;
10 import javax
.mail
.internet
.InternetAddress
;
11 import javax
.mail
.internet
.MimeMessage
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
18 /** Sends a mail via JavaMail, local mail command or Google Mail. */
19 public class SendMail
implements Runnable
{
21 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
22 // http://java.sun.com/products/javamail/FAQ.html#gmail
24 private final static Log log
= LogFactory
.getLog(SendMail
.class);
29 private String subject
;
31 private String username
;
32 private String password
;
33 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
36 if ("local".equals(host
))
37 sendWithMailCommand();
38 else if ("smtp.gmail.com".equals(host
))
44 protected void sendWithMailCommand() {
45 SystemCall mail
= new SystemCall("mail");
46 mail
.arg("-s", subject
).arg(to
);
48 if (log
.isDebugEnabled())
49 log
.debug("Sent mail to " + to
+ " with OS mail command");
52 protected void sendWithJavaMail() {
54 // Get system properties
55 Properties props
= System
.getProperties();
58 props
.put("mail.smtp.host", host
);
60 for (String key
: javaMailProperties
.keySet())
61 props
.put(key
, javaMailProperties
.get(key
));
64 Session session
= Session
.getDefaultInstance(props
, null);
67 MimeMessage message
= new MimeMessage(session
);
68 buildJavaMailMessage(message
);
71 Transport
.send(message
);
72 if (log
.isDebugEnabled())
73 log
.debug("Sent mail to " + to
+ " with JavaMail");
74 } catch (Exception e
) {
75 throw new SlcException("Cannot send message.", e
);
79 protected void sendWithGMail() {
81 Properties props
= new Properties();
82 props
.put("mail.smtps.auth", "true");
83 props
.put("mail.smtps.host", host
);
84 Session session
= Session
.getDefaultInstance(props
, null);
85 MimeMessage message
= new MimeMessage(session
);
86 buildJavaMailMessage(message
);
87 Transport t
= session
.getTransport("smtps");
89 t
.connect(host
, username
, password
);
90 t
.sendMessage(message
, message
.getAllRecipients());
94 if (log
.isDebugEnabled())
95 log
.debug("Sent mail to " + to
+ " with Google Mail");
96 } catch (Exception e
) {
97 throw new SlcException("Cannot send message.", e
);
101 protected void buildJavaMailMessage(Message message
) throws Exception
{
102 message
.setFrom(new InternetAddress(from
));
103 message
.addRecipient(Message
.RecipientType
.TO
, new InternetAddress(to
));
104 message
.setSubject(subject
);
105 message
.setText(text
);
108 public void setHost(String host
) {
112 public void setFrom(String from
) {
116 public void setTo(String to
) {
120 public void setSubject(String subject
) {
121 this.subject
= subject
;
124 public void setText(String text
) {
128 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
129 this.javaMailProperties
= javaMailProperties
;
132 public void setUsername(String username
) {
133 this.username
= username
;
136 public void setPassword(String password
) {
137 this.password
= password
;