package com.socrata.datasync;

import com.socrata.datasync.config.userpreferences.UserPreferences;
import com.socrata.datasync.config.userpreferences.UserPreferencesJava;
import com.sun.mail.smtp.SMTPTransport;

//import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Sends emails using Google SMTP
 *
 * @author doraemon
 */
public class SMTPMailer {
	
    private SMTPMailer() {
    	
    }

    /**
     * Send email using GMail SMTP server.
     * NOTE: obtains SMTP settings from userPrefs
     *
     * @param recipientEmail TO recipient
     * @param title title of the message
     * @param message message to be sent
     * @throws AddressException if the email address parse failed
     * @throws MessagingException if the connection is dead or not in the connected state 
     * 							   or if the message is not a MimeMessage
     */
    public static void send(String recipientEmail, String title, String message)
    		throws AddressException, MessagingException {
        SMTPMailer.send(recipientEmail, "", title, message);
    }

    /**
     * Send email using an SMTP server.
     *
     * @param recipientEmail TO recipient
     * @param ccEmail CC recipient. Can be empty if there is no CC recipient
     * @param title title of the message
     * @param message message to be sent
     * @throws AddressException if the email address parse failed
     * @throws MessagingException if the connection is dead or not in the connected state
     * 							   or if the message is not a MimeMessage
     */
    public static void send(String recipientEmail, String ccEmail, String title, String message) 
    		throws AddressException, MessagingException {
    	UserPreferences userPrefs = new UserPreferencesJava();
    	
        //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtps.host", userPrefs.getOutgoingMailServer());
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", userPrefs.getSmtpPort());
        String sslPort = userPrefs.getSslPort();
        if(!sslPort.equals("")) {
        	props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        	props.setProperty("mail.smtp.socketFactory.port", sslPort);
        }
        props.setProperty("mail.smtps.auth", "true");

        /*
        If set to false, the QUIT command is sent and the connection is immediately closed. If set 
        to true (the default), causes the transport to wait for the response to the QUIT command.

        ref :   http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
                http://forum.java.sun.com/thread.jspa?threadID=5205249
                smtpsend.java - demo program from javamail
        */
        props.put("mail.smtps.quitwait", "false");

        Session session = Session.getInstance(props, null);

        // -- Create a new message --
        final MimeMessage msg = new MimeMessage(session);

        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(userPrefs.getSmtpUsername()));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

        if (ccEmail.length() > 0) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
        }

        msg.setSubject(title);
        msg.setText(message, "utf-8");
        msg.setSentDate(new Date());

        SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
        
        t.connect(userPrefs.getOutgoingMailServer(), userPrefs.getSmtpUsername(), userPrefs.getSmtpPassword());
        t.sendMessage(msg, msg.getAllRecipients());      
        t.close();
    }
}

@@ -2497,24 +2497,25 @@
 Port();%0A
+%0A
         
 if(!sslP
@@ -2510,34 +2510,72 @@
     
-if(!sslPort.equals(%22%22)
+boolean useSSL = !(sslPort.equals(%22%22));%0A%0A%0A        if(useSSL
 ) %7B%0A
+%0A
     
@@ -2643,33 +2643,33 @@
 ACTORY);%0A       
- 
+%09
 %09props.setProper
@@ -2724,19 +2724,14 @@
     
- %7D%0A
      
-   
+%09
 prop
@@ -2768,25 +2768,28 @@
 %22, %22true%22);%0A
-%0A
+    
         /*%0A 
@@ -2885,17 +2885,16 @@
 . If set
- 
 %0A       
@@ -3232,24 +3232,29 @@
  */%0A        
+    %09
 props.put(%22m
@@ -3283,16 +3283,26 @@
 false%22);
+%0A        %7D
 %0A%0A      
@@ -3915,24 +3915,25 @@
 w Date());%0A%0A
+%0A
         SMTP
@@ -3945,16 +3945,97 @@
 port t =
+ (SMTPTransport)session.getTransport(%22smtp%22);%0A        if (useSSL)%0A            t =
  (SMTPTr

Add support for non-SSL SMTP servers