import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.*;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import java.util.*;

public class MainDriver {
	
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_PORT = 465;
    // private static final String SMTP_AUTH_USER = "<username>@g.clemson.edu";
    // *** uncomment the above line after putting
    // *** in your username where specified 

	
	public static void main(String[] args) {
		try {
			Properties props = new Properties();
			props.put("mail.transport.protocol", "smtp");
	        props.put("mail.smtp.host", SMTP_HOST_NAME);
	        props.put("mail.smtp.port", SMTP_PORT);
	        
	        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
	        props.put("mail.smtp.socketFactory.class", 
	        		"javax.net.ssl.SSLSocketFactory");
	        props.put("mail.smtp.auth", "true");
	        
	        Authenticator auth = new Authenticator () {
				public PasswordAuthentication getPasswordAuthentication(){
					return new PasswordAuthentication(SMTP_AUTH_USER, getPassword());
				}
			};
			
			Session session = Session.getDefaultInstance(props, auth);
			session.setDebug(true);
			
			Message msg = new MimeMessage(session);
			msg.setRecipient(RecipientType.TO, new InternetAddress("chochri@clemson.edu"));
			msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
			msg.setSubject("Hopefully, my last test message");
			msg.setText("Dear self, \n\nHopefully, soon I will stop sending email messages to me.");
			
			Transport.send(msg);
		    System.out.println("Message sent.");
			
		} catch(Exception exc) {
			System.out.println("Exception: " + exc);
		}
	}
	
	public static String getPassword()
	{
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Enter a password:");
		JPasswordField pwd = new JPasswordField(30);
		panel.add(label);
		panel.add(pwd);
		String[] options = new String[]{"OK", "Cancel"};
		int option = JOptionPane.showOptionDialog(null, panel, "Enter your password",
		                         JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
		                         null, options, options[0]);
		if(option == 0) // pressing OK button
		{
		    System.out.println("Entered password... ");
		    String m_pwd = new String(pwd.getPassword());
		    return m_pwd;
		}
		return null;
	}
}

