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

import it.mice.voila.runtime.mail.rmi.MailService;
import it.mice.voila.runtime.mail.rmi.MimeMessageBean;

import java.io.ByteArrayOutputStream;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;


public class ServiceTransportMimeMailSenderImpl extends MimeMailSenderImpl {
	private MailService mailService;
	
	/**
	 * @return the mailService
	 */
	public MailService getMailService() {
		return mailService;
	}

	/**
	 * @param mailService the mailService to set
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	/* (non-Javadoc)
	 * @see org.springframework.mail.javamail.JavaMailSenderImpl#send(javax.mail.internet.MimeMessage)
	 */
	@Override
	public void send(MimeMessage mimeMessage) throws MailException {
		MimeMessageBean mimeMessageBean = new MimeMessageBean();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			mimeMessage.writeTo(baos);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		mimeMessageBean.setMimeMessage(baos.toByteArray());
		
		getMailService().sendMail(mimeMessageBean, getMailConfig().getMailConfigId());
	}
}
