/*
 * Created on 2005.08.31.
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.etixpert.evolution.gui.servlets;

import javax.servlet.http.*;
import org.hibernate.*;
import java.util.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import com.etixpert.evolution.*;
import com.etixpert.evolution.gui.servlets.*;

import org.apache.commons.fileupload.*;
import java.io.*;
import org.apache.log4j.*;
import javax.activation.DataSource;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.jstl.fmt.*;


/**
 * @author Peter Albert
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class MyMailSender {

	private Message mess;
	private List feedbacks= new ArrayList();
    
    //private String from = "office@evolution-international.com";
    //private String reply = "sekretariat@evolution-international.com";
	
	private String from = "bestellung@evolution-international.com";
	private String reply = "bestellung@evolution-international.com";
	
    private Logger logger;
    
	private  class Sender implements Runnable {
    	
		private List dbEmails;
    	private String [] tos;
    	
		Sender(String[] tos) {
			this.tos = tos;
    		
		}
    	
		public void run() {
			feedbacks = new ArrayList();
        	
			if (tos!=null && tos.length>0) {
				for (int i=0; i<tos.length; i++) {
					try {
						sendMessage(mess,tos[i], feedbacks);
					} catch (Exception e) {
						logger.info("sending e-mail error: address:"+tos[i]+" error: "+e.getMessage());
					}
					
				}
        	    
			}
        	
			//reply(mess, feedbacks);
        	
		}
	}	
	/**
	 * 
	 */
	public MyMailSender(Logger logger) {
		super();
		
		this.logger = logger;
		
		// TODO Auto-generated constructor stub
	}

	public void send(String email, String order_id, String text) throws Exception  {
		String [] tos = new String[] {email};
		logger.info("1");
		ResourceBundle bundle = ResourceBundle.getBundle("evolution");
		logger.info("1");
       
		String smtp = bundle.getString("smtp");
		logger.info("1");
		java.util.Properties props = System.getProperties();
		logger.info("1");
		props.put("mail.smtp.host", smtp);
		props.put("mail.smtp.localhost", "localhost");
		props.put("mail.transport.protocol", "smtp");		
		logger.info("1");
		javax.mail.Session mailSession= javax.mail.Session.getDefaultInstance(props, null);
		mess =new MimeMessage(mailSession);
		mess.setFrom(new InternetAddress(from));
		mess.setReplyTo(new InternetAddress[] {new InternetAddress(reply)});
		logger.info("1");
		mess.setSubject("Bestellung "+order_id+" wurde erfolgreich angelegt");
		MimeMultipart mp = new MimeMultipart();
		MimeBodyPart bp = new MimeBodyPart();
		logger.info("1");
		bp.setContent(text, "text/html");
		//bp.setText(text);
		bp.setDisposition(MimeBodyPart.INLINE);
		
		mp.addBodyPart(bp);
		logger.info("1");
		
		mess.setContent(mp);
		Sender sender = new Sender(tos);
		sender.run();
		logger.info("1");
				
	}
	
	
	public  void sendMessage(Message mess, String address, List sent_reports) throws Exception {
		String id="";
		String lastname="";
      
		 String[] tos= new String [] {address};
		 
		 
			 
			 
            
			 mess.setRecipient(Message.RecipientType.TO, new InternetAddress(address));
			 Transport.send(mess);
			
			 	 
		 		
	  }	

}
