package org.sistema.spring.email;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Main App, gets an instance from Spring context and sends an email.
 * 
 * @author Spring
 * @greetz any
 */
public class App {

	static Properties prop;

	private static void loadProperties() {
		prop = new Properties();

		try {
			prop.load(new FileInputStream(
					"src/main/resources/emailer.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"springmailer.xml");
		loadProperties();
		Emailer emailer = (Emailer) context.getBean("emailer");

		System.out.println("Sending email: ");

		emailer.setFrom(prop.getProperty("email.username"));
		emailer.setTo("PUT_TO_VALUE@anyother.com");	
		emailer.setSubject("Testing Spring email, versión 4");
		emailer.setText("Testing email.");
		emailer.addAttachment(context.getResource("6666_1.jpg"));

		if (emailer.send()) {
			System.out.println("OK Email Sent! " + emailer.toString());
		} else {
			System.err.println("Error, something went wrong");
		}
	}
}