package it.mice.voila.runtime.mail.rmi;

import it.mice.voila.runtime.mail.manager.MailManager;
import it.mice.voila.runtime.mail.senders.MimeMailSenderImpl;
import it.mice.voila.runtime.mail.utils.MailUtils;

import java.io.ByteArrayInputStream;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class MailServiceImpl implements MailService {
	/**
	 * Action default logger
	 */
	private static Log logger = LogFactory.getLog(MailServiceImpl.class);

	@Autowired
	private MailManager mailSenderManager;
	
	public void sendMail(MimeMessageBean mimeMessageBean, String mailConfigId) {
		MimeMailSenderImpl mailSender = MailUtils.fetchMailSender(mailSenderManager, mailConfigId);
		MimeMessage mimeMessage;
		try {
			mimeMessage = new MimeMessage(mailSender.getSession(), new ByteArrayInputStream(mimeMessageBean.getMimeMessage()));
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
		mailSender.send(mimeMessage);
	}

}
