package com.eassessment.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {
//	static String ADMIN_EMIAL_ID = "support@anandmunshi.com";
//	static String ADMIN_EMAIL_PASSWORD = "Anand@2016";
	private static String ADMIN_EMIAL_ID = "info@anandmunshi.com";
	private static String ADMIN_EMAIL_PASSWORD = "Omprakash1!";
	
	private static String TO_EMAIL="info@anandmunshi.com";
//	private static String BCC_EMAIL="info@anandmunshi.com";
	private static String BCC_EMAIL="anandmunshi@hotmail.com, info@anandmunshi.com";
	 
	public static void main(String[] args) {
 	System.out.println(SendMailTLS(TO_EMAIL, new StringBuilder("testtest"),"issuefix"));
		
		/* try {
			String filename = "E:\\J2eeWorkspace\\emyassessment\\WebContent\\WEB-INF\\newreg.txt";
			StringBuilder sb = new StringBuilder();
			File file = new File(filename);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String s;
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
			br.close();

			System.out.println("Email Ready");
			System.out.println(SendMailTLS("@gmail.com", sb,"Hi.."));
			//Email.sendMail(TO_EMAIL, "",  sb);

		} catch (Exception e) {
			e.printStackTrace();
		}*/
 
	}

	public static String sendMail(String toMailId, String msg,
			StringBuilder emailContent, String subject) {
		return null;
		/*Properties props = new Properties();
		props.put("mail.smtp.host", "host3.dns2dns.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		

		javax.mail.Session session = javax.mail.Session.getDefaultInstance(
				props, new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(ADMIN_EMIAL_ID,
								ADMIN_EMAIL_PASSWORD);
					}
				});

		try {

			Message message = new MimeMessage(session);
			// message.setFrom(new InternetAddress("from@no-spam.com"));
			message.setFrom(new InternetAddress("miracle.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(toMailId));			
			message.setSubject(subject);

			// message.setContent(
			// "<h1>You have received the Friend Request on Live Location by </h1> "+" "+
			// requestSender
			// +
			// "<br /> <a href='https://play.google.com/store/apps/details?id=com.androidhiv.pushnotifications'><h3>Accept</h3></a>",
			// "text/html");

			message.setContent(emailContent.toString(), "text/html");

			 message.setContent(emailContent.toString()+msg+StringUtil.tailEmail,"text/html");
			 
//			 Transport tr = session.getTransport("smtp");
//			 tr.connect("smtp.gmail.com", ADMIN_EMIAL_ID, ADMIN_EMAIL_PASSWORD);
//			 message.saveChanges(); // don't forget this
//			 tr.sendMessage(message, message.getAllRecipients());
//			 tr.close();
			 
			Transport.send(message);

			System.out.println("Done");
			return Constants.SUCCESS;

		} catch (MessagingException e) {
			e.printStackTrace();
			return Constants.FAIL;

		}*/
	}
	 
	public static String SendMailTLS(String toMailId ,
			StringBuilder emailContent, String subject) {
		
		System.out.println("To Mail::"+toMailId);
		System.out.println("From Mail::"+ADMIN_EMIAL_ID);
		System.out.println("BCC Mail::"+BCC_EMAIL);
		final String username = ADMIN_EMIAL_ID;
		final String password = ADMIN_EMAIL_PASSWORD;

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "mail.anandmunshi.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(ADMIN_EMIAL_ID));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(toMailId));
			message.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(BCC_EMAIL));
			message.setSubject(subject);
			
//			message.setText("Dear Mail Crawler,"
//				+ "\n\n No spam to my email, please!");
			message.setContent(emailContent.toString(),"text/html"); 
			Transport.send(message);

			System.out.println("Email sent to:");
			return "Email sent successfully";
		} catch (MessagingException e) {
			e.printStackTrace();
			return "Error TSL"+e.getMessage();
		}catch (Exception e) {
			e.printStackTrace();
			return "Error TSL"+e.getMessage();
		}
	}
}