X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=legacy%2Forg.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Fmail%2FSendMail.java;fp=legacy%2Forg.argeo.slc.support%2Fsrc%2Forg%2Fargeo%2Fslc%2Fmail%2FSendMail.java;h=db4ab45f9fc047db21f21b0728a3d75352dcd9ee;hb=6fc94d69efe089414ac9e63bde3efab1cbf7b7ca;hp=0000000000000000000000000000000000000000;hpb=b36c62642bd0db11b3133b369cc026fd4b7a1ec6;p=gpl%2Fargeo-slc.git diff --git a/legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java b/legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java new file mode 100644 index 000000000..db4ab45f9 --- /dev/null +++ b/legacy/org.argeo.slc.support/src/org/argeo/slc/mail/SendMail.java @@ -0,0 +1,140 @@ +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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.argeo.slc.core.execution.tasks.SystemCall; + +/** Sends a mail via JavaMail, local mail command or Google Mail. */ +public class SendMail implements Runnable { + // See: + // http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage + // http://java.sun.com/products/javamail/FAQ.html#gmail + + private final static Log log = LogFactory.getLog(SendMail.class); + + private String host; + private String from; + private String to; + private String subject; + private String text; + private String username; + private String password; + private Map javaMailProperties = new HashMap(); + + 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(); + if (log.isDebugEnabled()) + log.debug("Sent mail to " + to + " with OS mail command"); + } + + 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); + if (log.isDebugEnabled()) + log.debug("Sent mail to " + to + " with JavaMail"); + } 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(); + } + if (log.isDebugEnabled()) + log.debug("Sent mail to " + to + " with Google Mail"); + } 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 javaMailProperties) { + this.javaMailProperties = javaMailProperties; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + +}