]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java
0f1eb3c990f01124973e6c65bc0cc4e89837b610
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
.argeo
.api
.cms
.CmsLog
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
17 /** Sends a mail via JavaMail, local mail command or Google Mail. */
18 public class SendMail
implements Runnable
{
20 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
21 // http://java.sun.com/products/javamail/FAQ.html#gmail
23 private final static CmsLog log
= CmsLog
.getLog(SendMail
.class);
28 private String subject
;
30 private String username
;
31 private String password
;
32 private Map
<String
, String
> javaMailProperties
= new HashMap
<String
, String
>();
35 if ("local".equals(host
))
36 sendWithMailCommand();
37 else if ("smtp.gmail.com".equals(host
))
43 protected void sendWithMailCommand() {
44 SystemCall mail
= new SystemCall("mail");
45 mail
.arg("-s", subject
).arg(to
);
47 if (log
.isDebugEnabled())
48 log
.debug("Sent mail to " + to
+ " with OS mail command");
51 protected void sendWithJavaMail() {
53 // Get system properties
54 Properties props
= System
.getProperties();
57 props
.put("mail.smtp.host", host
);
59 for (String key
: javaMailProperties
.keySet())
60 props
.put(key
, javaMailProperties
.get(key
));
63 Session session
= Session
.getDefaultInstance(props
, null);
66 MimeMessage message
= new MimeMessage(session
);
67 buildJavaMailMessage(message
);
70 Transport
.send(message
);
71 if (log
.isDebugEnabled())
72 log
.debug("Sent mail to " + to
+ " with JavaMail");
73 } catch (Exception e
) {
74 throw new SlcException("Cannot send message.", e
);
78 protected void sendWithGMail() {
80 Properties props
= new Properties();
81 props
.put("mail.smtps.auth", "true");
82 props
.put("mail.smtps.host", host
);
83 Session session
= Session
.getDefaultInstance(props
, null);
84 MimeMessage message
= new MimeMessage(session
);
85 buildJavaMailMessage(message
);
86 Transport t
= session
.getTransport("smtps");
88 t
.connect(host
, username
, password
);
89 t
.sendMessage(message
, message
.getAllRecipients());
93 if (log
.isDebugEnabled())
94 log
.debug("Sent mail to " + to
+ " with Google Mail");
95 } catch (Exception e
) {
96 throw new SlcException("Cannot send message.", e
);
100 protected void buildJavaMailMessage(Message message
) throws Exception
{
101 message
.setFrom(new InternetAddress(from
));
102 message
.addRecipient(Message
.RecipientType
.TO
, new InternetAddress(to
));
103 message
.setSubject(subject
);
104 message
.setText(text
);
107 public void setHost(String host
) {
111 public void setFrom(String from
) {
115 public void setTo(String to
) {
119 public void setSubject(String subject
) {
120 this.subject
= subject
;
123 public void setText(String text
) {
127 public void setJavaMailProperties(Map
<String
, String
> javaMailProperties
) {
128 this.javaMailProperties
= javaMailProperties
;
131 public void setUsername(String username
) {
132 this.username
= username
;
135 public void setPassword(String password
) {
136 this.password
= password
;