-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.argeo.slc</groupId>
<artifactId>com.springsource.com.jcraft.jsch</artifactId>
</dependency>
+ <!-- Scheduling -->
<dependency>
<groupId>com.opensymphony.quartz</groupId>
<artifactId>com.springsource.org.quartz</artifactId>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context.support</artifactId>
</dependency>
+
+ <!-- Mail -->
+ <dependency>
+ <groupId>javax.mail</groupId>
+ <artifactId>com.springsource.javax.mail</artifactId>
+ </dependency>
+
+ <!-- TODO: check if necessary here -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
--- /dev/null
+package org.argeo.slc.mail;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.mail.Message;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.tasks.SystemCall;
+
+// http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
+// http://java.sun.com/products/javamail/FAQ.html#gmail
+public class SendMail implements Runnable {
+ private String host;
+ private String from;
+ private String to;
+ private String subject;
+ private String text;
+ private String username;
+ private String password;
+ private Map<String, String> javaMailProperties = new HashMap<String, String>();
+
+ public void run() {
+ if ("local".equals(host))
+ sendWithMailCommand();
+ else if ("smtp.gmail.com".equals(host))
+ sendWithGMail();
+ else
+ sendWithJavaMail();
+ }
+
+ protected void sendWithMailCommand() {
+ SystemCall mail = new SystemCall("mail");
+ mail.arg("-s", subject).arg(to);
+ mail.run();
+ }
+
+ protected void sendWithJavaMail() {
+ try {
+ // Get system properties
+ Properties props = System.getProperties();
+
+ // Setup mail server
+ props.put("mail.smtp.host", host);
+
+ for (String key : javaMailProperties.keySet())
+ props.put(key, javaMailProperties.get(key));
+
+ // Get session
+ Session session = Session.getDefaultInstance(props, null);
+
+ // Define message
+ MimeMessage message = new MimeMessage(session);
+ buildJavaMailMessage(message);
+
+ // Send message
+ Transport.send(message);
+ } catch (Exception e) {
+ throw new SlcException("Cannot send message.", e);
+ }
+ }
+
+ protected void sendWithGMail() {
+ try {
+ Properties props = new Properties();
+ props.put("mail.smtps.auth", "true");
+ props.put("mail.smtps.host", host);
+ Session session = Session.getDefaultInstance(props, null);
+ MimeMessage message = new MimeMessage(session);
+ buildJavaMailMessage(message);
+ Transport t = session.getTransport("smtps");
+ try {
+ t.connect(host, username, password);
+ t.sendMessage(message, message.getAllRecipients());
+ } finally {
+ t.close();
+ }
+
+ } catch (Exception e) {
+ throw new SlcException("Cannot send message.", e);
+ }
+ }
+
+ protected void buildJavaMailMessage(Message message) throws Exception {
+ message.setFrom(new InternetAddress(from));
+ message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
+ message.setSubject(subject);
+ message.setText(text);
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public void setFrom(String from) {
+ this.from = from;
+ }
+
+ public void setTo(String to) {
+ this.to = to;
+ }
+
+ public void setSubject(String subject) {
+ this.subject = subject;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public void setJavaMailProperties(Map<String, String> javaMailProperties) {
+ this.javaMailProperties = javaMailProperties;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+}