package de.hsadmin.core.qserv;

import java.util.Properties;

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

public class MailerProcessor extends AbstractProcessor {

	private static final long serialVersionUID = -8900943664576420041L;

	private String aTo;
	private String aSubject;
	private String aBody;

	public MailerProcessor(String aTo, String aSubject, String aBody) {
		this.aTo = aTo;
		this.aSubject = aSubject;
		this.aBody = aBody;
	}

	public Object process() throws ProcessorException {
		Properties aProps = new Properties();
		aProps.setProperty("mail.smtp.host", "localhost");

		Session aSess = Session.getInstance(aProps);
		MimeMessage aMsg = new MimeMessage(aSess);
		try {
			aMsg.setFrom(new InternetAddress("robot@hostsharing.net"));
			aMsg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(aTo));
			aMsg.setSubject(aSubject);
			aMsg.setText(aBody);
			MailerShell.send(aMsg);
			return null;
		} catch (MessagingException aExc) {
			throw new ProcessorException(aExc);
		}
	}
}
