package com.javahonk;


import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class SendEmailVelocityTemplate {
	
	public static void main(String[] args) {
		sendEmailToGmailAccount();
	}

	private static void sendEmailToGmailAccount() {
		
		try {

			InternetAddress[] distributionList = InternetAddress.parse("javahonk@gmail.com,javahonk@javahonk.com",false);
			String from = "javahonk@gmail.com";
			String subject = "Test email";

			Properties props = new Properties();
			props.put("mail.smtp.host", "localhost");
			props.put("mail.smtp.port", "25");
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(false);
			
			Message msg = new MimeMessage(session);
			String message = "<div style=\"color:red;\">Email with dynamic template</div></br></br>"+createHTMLTemplate();
			message = message+"<div style=\"color:red;\">Email with dynamic list template</div></br></br>"+createHTMLTemplateWithList();
			message = message +"<div style=\"color:red;\">Email with dynamic map template</div></br></br>"+createHTMLTemplateWithMap();
			msg.setContent(message, "text/html; charset=utf-8");
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, distributionList);
			msg.setSubject(subject);
			msg.setSentDate(new Date());
			Transport.send(msg);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private static String createHTMLTemplate(){
		
		VelocityEngine ve = new VelocityEngine();
		ve.init();
		Template t = ve.getTemplate("./src/main/resources/templates/Sample.vm");
		VelocityContext vc = new VelocityContext();
		vc.put("firstName", "Email");
		vc.put("lastName", "Test");
		vc.put("age", "30");
		StringWriter sw = new StringWriter();
		t.merge(vc, sw);
		return sw.toString();
	}
	
	private static String createHTMLTemplateWithList(){
		
		VelocityEngine ve = new VelocityEngine();
		ve.init();
		Template t = ve.getTemplate("./src/main/resources/templates/SampleRenderList.vm");
		VelocityContext vc = new VelocityContext();
		vc.put("firstName", "Test Email");
		List<String> bar = new ArrayList<String>();
		bar.add("Test");
		bar.add("Test");
		bar.add("Test");
		vc.put("bar", bar);
		StringWriter sw = new StringWriter();
		t.merge(vc, sw);
		return sw.toString();
	}
	
	private static String createHTMLTemplateWithMap(){
		
		VelocityEngine ve = new VelocityEngine();
		ve.init();
		Template t = ve.getTemplate("./src/main/resources/templates/SampleRenderMap.vm");
		VelocityContext vc = new VelocityContext();
		List<Map<String, Object>> bar = new ArrayList<Map<String, Object>>();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Test", "Value1");
		data.put("Tes2", "Value2");
		bar.add(data);
		vc.put("bar", bar);
		StringWriter sw = new StringWriter();
		t.merge(vc, sw);
		return sw.toString();
	}
}