package de.hsadmin.core.qserv;

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

public class MailerShell {
	
	private static boolean bExecute = true; // really execute or just store
											// command and stdin?
	private static MimeMessage aMessage; // stored message

	/**
	 * Set mode of real mailer or just storing the messages for testing
	 * purposes.
	 * 
	 * @param bExec
	 *            specifies whether messages are really send (true) or just
	 *            stored (false)
	 */
	public static void setExecute(boolean bExec) {
		bExecute = bExec;
	}

	/**
	 * Returns and clears the last command which should have been executed.
	 * 
	 * @return Last command, plus "<<EOF\n" + stdin + "EOF" if stdin was given.
	 */
	public static MimeMessage popLastMessage() {
		MimeMessage aLastMessage = aMessage;
		aMessage = null;
		return aLastMessage;
	}

	public static void send(MimeMessage aMsg) throws MessagingException {
		if (bExecute)
			javax.mail.Transport.send(aMsg);
		aMessage = aMsg;
	}

}
