package org.dainst.gazetteer.helpers;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

public class MailService {

	@Value("${senderMail}")
	private String senderMail;
	
	@Value("${smtpHost}")
	private String smtpHost;
	
	@Value("${smtpPort}")
	private String smtpPort;
	
	@Value("${mailUsername}")
	private String mailUsername;
	
	@Value("${mailPassword}")
	private String mailPassword;
	
	private static Logger logger = LoggerFactory.getLogger(MailService.class);
	
	public void sendMail(String recipientMail, String subject, String content) throws MessagingException {
		
		sendMail(recipientMail, subject, content, null);
	}
	
	public void sendMail(String recipientMail, String subject, String content, String replyTo) throws MessagingException {
		
		if (senderMail.isEmpty() || smtpHost.isEmpty() || smtpPort.isEmpty() || mailUsername.isEmpty() || mailPassword.isEmpty()) {
			logger.warn("Could not send mail: Mail properties not set");
			return;
		}
		
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.host", smtpHost);
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.port", smtpPort);
		Session session = Session.getInstance(properties);
		
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(senderMail));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientMail));
		if (replyTo != null) message.setReplyTo(new InternetAddress[] {new InternetAddress(replyTo)});
		message.setSubject(subject);
		message.setContent(content, "text/html; charset=utf-8");
		
	    Transport transport = session.getTransport("smtp");
	    try {
	        transport.connect(mailUsername, mailPassword);
	        transport.sendMessage(message, message.getAllRecipients());
	    } finally {
	    	transport.close();
	    }
	}

	public String getSenderMail() {
		return senderMail;
	}

	public void setSenderMail(String senderMail) {
		this.senderMail = senderMail;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	public String getSmtpPort() {
		return smtpPort;
	}

	public void setSmtpPort(String smtpPort) {
		this.smtpPort = smtpPort;
	}
}

@@ -1937,16 +1937,120 @@
 nts());%0A
+%09    %7D catch (MessagingException e) %7B%0A%09    %09logger.warn(%22Could not send mail to: %22 + recipientMail, e);%0A
 %09    %7D f

Fix error handling in MailService