]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java
0f1eb3c990f01124973e6c65bc0cc4e89837b610
[gpl/argeo-slc.git] / legacy / org.argeo.slc.support / src / org / argeo / slc / mail / SendMail.java
1 package org.argeo.slc.mail;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Properties;
6
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;
12
13 import org.argeo.api.cms.CmsLog;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.core.execution.tasks.SystemCall;
16
17 /** Sends a mail via JavaMail, local mail command or Google Mail. */
18 public class SendMail implements Runnable {
19 // See:
20 // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
21 // http://java.sun.com/products/javamail/FAQ.html#gmail
22
23 private final static CmsLog log = CmsLog.getLog(SendMail.class);
24
25 private String host;
26 private String from;
27 private String to;
28 private String subject;
29 private String text;
30 private String username;
31 private String password;
32 private Map<String, String> javaMailProperties = new HashMap<String, String>();
33
34 public void run() {
35 if ("local".equals(host))
36 sendWithMailCommand();
37 else if ("smtp.gmail.com".equals(host))
38 sendWithGMail();
39 else
40 sendWithJavaMail();
41 }
42
43 protected void sendWithMailCommand() {
44 SystemCall mail = new SystemCall("mail");
45 mail.arg("-s", subject).arg(to);
46 mail.run();
47 if (log.isDebugEnabled())
48 log.debug("Sent mail to " + to + " with OS mail command");
49 }
50
51 protected void sendWithJavaMail() {
52 try {
53 // Get system properties
54 Properties props = System.getProperties();
55
56 // Setup mail server
57 props.put("mail.smtp.host", host);
58
59 for (String key : javaMailProperties.keySet())
60 props.put(key, javaMailProperties.get(key));
61
62 // Get session
63 Session session = Session.getDefaultInstance(props, null);
64
65 // Define message
66 MimeMessage message = new MimeMessage(session);
67 buildJavaMailMessage(message);
68
69 // Send 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);
75 }
76 }
77
78 protected void sendWithGMail() {
79 try {
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");
87 try {
88 t.connect(host, username, password);
89 t.sendMessage(message, message.getAllRecipients());
90 } finally {
91 t.close();
92 }
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);
97 }
98 }
99
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);
105 }
106
107 public void setHost(String host) {
108 this.host = host;
109 }
110
111 public void setFrom(String from) {
112 this.from = from;
113 }
114
115 public void setTo(String to) {
116 this.to = to;
117 }
118
119 public void setSubject(String subject) {
120 this.subject = subject;
121 }
122
123 public void setText(String text) {
124 this.text = text;
125 }
126
127 public void setJavaMailProperties(Map<String, String> javaMailProperties) {
128 this.javaMailProperties = javaMailProperties;
129 }
130
131 public void setUsername(String username) {
132 this.username = username;
133 }
134
135 public void setPassword(String password) {
136 this.password = password;
137 }
138
139 }