View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.mail;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import javax.mail.Message;
23  import javax.mail.Session;
24  import javax.mail.Transport;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeMessage;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.argeo.slc.SlcException;
31  import org.argeo.slc.core.execution.tasks.SystemCall;
32  
33  /** Sends a mail via JavaMail, local mail command or Google Mail. */
34  public class SendMail implements Runnable {
35  	// See:
36  	// http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailUsage
37  	// http://java.sun.com/products/javamail/FAQ.html#gmail
38  
39  	private final static Log log = LogFactory.getLog(SendMail.class);
40  
41  	private String host;
42  	private String from;
43  	private String to;
44  	private String subject;
45  	private String text;
46  	private String username;
47  	private String password;
48  	private Map<String, String> javaMailProperties = new HashMap<String, String>();
49  
50  	public void run() {
51  		if ("local".equals(host))
52  			sendWithMailCommand();
53  		else if ("smtp.gmail.com".equals(host))
54  			sendWithGMail();
55  		else
56  			sendWithJavaMail();
57  	}
58  
59  	protected void sendWithMailCommand() {
60  		SystemCall mail = new SystemCall("mail");
61  		mail.arg("-s", subject).arg(to);
62  		mail.run();
63  		if (log.isDebugEnabled())
64  			log.debug("Sent mail to " + to + " with OS mail command");
65  	}
66  
67  	protected void sendWithJavaMail() {
68  		try {
69  			// Get system properties
70  			Properties props = System.getProperties();
71  
72  			// Setup mail server
73  			props.put("mail.smtp.host", host);
74  
75  			for (String key : javaMailProperties.keySet())
76  				props.put(key, javaMailProperties.get(key));
77  
78  			// Get session
79  			Session session = Session.getDefaultInstance(props, null);
80  
81  			// Define message
82  			MimeMessage message = new MimeMessage(session);
83  			buildJavaMailMessage(message);
84  
85  			// Send message
86  			Transport.send(message);
87  			if (log.isDebugEnabled())
88  				log.debug("Sent mail to " + to + " with JavaMail");
89  		} catch (Exception e) {
90  			throw new SlcException("Cannot send message.", e);
91  		}
92  	}
93  
94  	protected void sendWithGMail() {
95  		try {
96  			Properties props = new Properties();
97  			props.put("mail.smtps.auth", "true");
98  			props.put("mail.smtps.host", host);
99  			Session session = Session.getDefaultInstance(props, null);
100 			MimeMessage message = new MimeMessage(session);
101 			buildJavaMailMessage(message);
102 			Transport t = session.getTransport("smtps");
103 			try {
104 				t.connect(host, username, password);
105 				t.sendMessage(message, message.getAllRecipients());
106 			} finally {
107 				t.close();
108 			}
109 			if (log.isDebugEnabled())
110 				log.debug("Sent mail to " + to + " with Google Mail");
111 		} catch (Exception e) {
112 			throw new SlcException("Cannot send message.", e);
113 		}
114 	}
115 
116 	protected void buildJavaMailMessage(Message message) throws Exception {
117 		message.setFrom(new InternetAddress(from));
118 		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
119 		message.setSubject(subject);
120 		message.setText(text);
121 	}
122 
123 	public void setHost(String host) {
124 		this.host = host;
125 	}
126 
127 	public void setFrom(String from) {
128 		this.from = from;
129 	}
130 
131 	public void setTo(String to) {
132 		this.to = to;
133 	}
134 
135 	public void setSubject(String subject) {
136 		this.subject = subject;
137 	}
138 
139 	public void setText(String text) {
140 		this.text = text;
141 	}
142 
143 	public void setJavaMailProperties(Map<String, String> javaMailProperties) {
144 		this.javaMailProperties = javaMailProperties;
145 	}
146 
147 	public void setUsername(String username) {
148 		this.username = username;
149 	}
150 
151 	public void setPassword(String password) {
152 		this.password = password;
153 	}
154 
155 }