/*
 * $Id: MailUtils.java,v 1.6 2008/02/12 09:39:54 korowa Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: MailUtils.java,v $
 * Revision 1.6  2008/02/12 09:39:54  korowa
 * + Eine Möglichkeit für den Benutzer hinzugefügt die Emailsart auszuwählen (HTML oder Plaintext)
 * + Betreff für den Messenger der als Emailsubject bei Benachrichtigung übers erscheinen von VKC Dokumenten verschickt wird.
 *
 * Revision 1.5  2007/10/30 13:32:42  marbobo
 * + removed unnecessary statements
 *
 * Revision 1.4  2006/10/04 03:14:29  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.3  2005/12/15 15:28:52  marbobo
 * + Benutzername und Passwort für Mailserver implementiert
 *
 * Revision 1.2  2004/07/20 08:33:14  jonyp
 * added copyright info + cvs log
 *
 * Revision 1.1  2004/05/28 10:43:34  teng
 * *** empty log message ***
 *
 *
 */
package vkc.misc;

import java.io.FileNotFoundException;
import java.util.Vector;

import javax.mail.MessagingException;
import javax.mail.SendFailedException;

import vkc.view.WebShare;
import vkc.db.entities.VKCUser;
import vkc.util.JavaMailUtils;
import vkc.util.ServletUtils;

/**
 * Diese Klasse bietet eine zusaetzliche Funktion fuer das Senden von Email mit VKC-Logo. 
 * @author teng
 */
public class MailUtils extends JavaMailUtils{
	private String vkcLogoPath = "templates/${template.name}/gfx/vkc-logo-blau.gif";
	/**
	 * contains one <code>MailImage</code>, the vkc logo, at this moment 
	 */
	private Vector images = null;
	private VKCUser user = null;

	private MailUtils() {
	}
	
	public MailUtils(VKCUser user) {
	    this();
		this.user = user;
	}
	
	/**
	 * Use this method for sending email with vkc-logo image (in the template file use html tag <img src="cid:0" > to reference the logo image part ). 
	 * This method will attach automatically the logo image in the email 
	 * @param sender email address of the sender
	 * @param recepient email address of the recepient
	 * @param subject
	 * @param asciiText the plain text part
	 * @param htmlText the html part 
	 * @param files contains files for attachment. If null, send without any attachment
	 * @param displayedFilename the name displayed for the attachment files. If null, the file name will be used.
	 * @param smtpServer the smtp server name
	 * @param smtpUsername the smtp server username
	 * @param smtpPassword the smtp server password
	 * @throws IllegalArgumentException
	 * @throws MessagingException
	 * @throws SendFailedException
	 */
	public void sendSMTPMailWithVKCLogo(String sender, String recepient, String subject, String asciiText, String htmlText, Vector files, String displayedFilename, String smtpServer, String smtpUsername, String smtpPassword) throws IllegalArgumentException, MessagingException, SendFailedException {
		initLogoImage();
		sendSMTPMail(sender, recepient, subject, asciiText, htmlText, files, displayedFilename, images, smtpServer, smtpUsername, smtpPassword);
	}   

	/**
	 * prepare the logo image 
	 *
	 */
	private void initLogoImage() {
		if (images == null) {
			images = new Vector(1); // at this moment we have only one image,
			// the logo
			MailImage mailImage = null;
			try {
				mailImage = new MailImage(WebShare.VKC_ROOT_PATH
						+ ServletUtils.replacePathVars(vkcLogoPath, user), 0);
				images.add(mailImage);
			} catch (FileNotFoundException ex) {
				images = null;
				VKCLogManager.infoLog(this, "Logo image file:"
						+ WebShare.VKC_ROOT_PATH
						+ ServletUtils.replacePathVars(vkcLogoPath, user)
						+ " not found!");
			}
		}
	}

	public void setUser(VKCUser user) {
		this.user = user;
	}
}