package com.tableausoftware.tabcmd.commands;

import com.tableausoftware.tabcmd.http.HttpRequest;
import com.tableausoftware.tabcmd.session.Session;
import com.tableausoftware.core.util.SMTPClientWithFile;
import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;



public class SendEmail extends Command {

    public String getName() {
        return "sendmail";
    }

    public String getUsage() {
        return "tabcmd sendmail [options]";
    }

    public String getShortDescription() {
        return "Send email from command line using SMTP server.";
    }

    public String getDescription() {
        return getShortDescription();
    }

    public boolean needsSession() {
        return false;
    }

    public HttpRequest execute(Session session, CommandLine commandLine, List<String> remainingArgs) {
        List<String> recipients = new ArrayList<String>();
        recipients.add("tamas.foldi@ge.com");

        try {
            SMTPClientWithFile client = new SMTPClientWithFile(null, null, commandLine.getOptionValue("smtp"), 25, false);

            client.sendMailWithFile();
        } catch (MessagingException ex){
            m_logger.error(ex);
        }
        return null;
    }


    public Options getOptions(Options options) {
        OptionBuilder.withLongOpt("smtp-host");
        OptionBuilder.withDescription("SMTP Server's address");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("SMTPSERVER");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("to");
        OptionBuilder.withDescription("Recipient addresses, separated by semicolon (;) character");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("TO_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("from");
        OptionBuilder.withDescription("From address");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("FROM_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("subject");
        OptionBuilder.withDescription("Subject of the email");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("TO_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("file");
        OptionBuilder.withDescription("File to include");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("FILENAME");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("body");
        OptionBuilder.withDescription("Body Text");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("EMAIL_TEXT_BODY");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-port");
        OptionBuilder.withDescription("SMTP Server's port for incoming communication");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("PORT");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-user");
        OptionBuilder.withDescription("User name for SMTP authentication");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("USER");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-passw");
        OptionBuilder.withDescription("SMTP User's password");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("PASSWORD");
        options.addOption(OptionBuilder.create(""));

        return options;
    }
}

@@ -199,16 +199,17 @@
 thFile;%0A
+%0A
 import j
@@ -242,36 +242,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -391,17 +391,16 @@
 ions;%0A%0A%0A
-%0A
 public c
@@ -956,283 +956,800 @@
     
-List%3CString%3E recipients = new ArrayList%3CString%3E();%0A        recipients.add(%22tamas.foldi@ge.com%22);%0A%0A        try %7B%0A            SMTPClientWithFile client = new SMTPClientWithFile(null, null, commandLine.getOptionValue(%22smtp%22), 25, false);%0A%0A            client.sendMailWithFile(
+int port = 25;%0A%0A        if (commandLine.getOptionValue(%22smtp-port%22) != null)%0A            port = Integer.parseInt(commandLine.getOptionValue(%22smtp-port%22));%0A%0A        try %7B%0A            SMTPClientWithFile client = new SMTPClientWithFile(%0A                    commandLine.getOptionValue(%22smtp-user%22),%0A                    commandLine.getOptionValue(%22smtp-passw%22),%0A                    commandLine.getOptionValue(%22smtp-host%22),%0A                    port,%0A                    true);%0A%0A            client.sendMailWithFile(%0A                    commandLine.getOptionValue(%22to%22),%0A                    commandLine.getOptionValue(%22from%22),%0A                    commandLine.getOptionValue(%22subject%22),%0A                    commandLine.getOptionValue(%22body%22),%0A                    commandLine.getOptionValue(%22file%22)
 );%0A
+%0A
     
@@ -1783,16 +1783,17 @@
 tion ex)
+ 
 %7B%0A      
@@ -1811,13 +1811,57 @@
 ger.
-error
+debug(ex);%0A            throw new RuntimeException
 (ex)

Integrate w/ SMTPClientWithFile