/*
 * Copyright (C) 2013 The enviroCar project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.envirocar.server.event.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailSSL implements SendMail {

	private String user;
	private String password;
	private String from;
	private String host;
	private int port;

	@Override
	public void send(String email, String subject, String content)
			throws MessagingException {
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.socketFactory.port", Integer.toString(port));
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", Integer.toString(port));

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(user, password);
					}
				});

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
		message.setSubject(subject);
		message.setText(content);

		Transport.send(message);
	}

	@Override
	public void setup(String user, String password, String fromEmail,
			String smtpHost, int smtpPort) {
		this.user = user;
		this.password = password;
		this.from = fromEmail;
		this.host = smtpHost;
		this.port = smtpPort;
	}

	@Override
	public void setup(Properties conf) {
		setup(conf.getProperty("USER"), conf.getProperty("PASSWORD"),
				conf.getProperty("FROM_MAIL"), conf.getProperty("SMTP_HOST"),
				Integer.parseInt(conf.getProperty("SMTP_PORT")));
	}
}
@@ -786,16 +786,49 @@
 rties;%0A%0A
+import javax.mail.Authenticator;%0A
 import j
@@ -1692,15 +1692,8 @@
 .get
-Default
 Inst
@@ -1707,28 +1707,13 @@
 ops,
-%0A%09%09%09%09new javax.mail.
+ new 
 Auth
@@ -1729,18 +1729,16 @@
 () %7B%0A%09%09%09
-%09%09
 protecte
@@ -1796,18 +1796,16 @@
 ) %7B%0A%09%09%09%09
-%09%09
 return n
@@ -1854,14 +1854,10 @@
 %0A%09%09%09
-%09%09%7D%0A%09%09
+%7D%0A
 %09%09%7D)
@@ -2000,17 +2000,21 @@
 Type.TO,
- 
+%0A%09%09%09%09
 Internet

disable session caching in javax.mail