import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.lavantech.net.mail.*;

public class MailCompDemo extends JFrame implements ActionListener
{
    JTextField fromTF, toTF, ccTF, bccTF, subTF, attachTF;
    JTextArea messageTA;
    JButton browseB, sendB, exitB;

    public MailCompDemo()
    {
        super("Mail Component Demo");
        getContentPane().setLayout(new BorderLayout());

        JPanel northPanel = new JPanel(new BorderLayout());
        getContentPane().add(northPanel, BorderLayout.NORTH);
        JPanel labelPanel = new JPanel(new GridLayout(0,1));
        northPanel.add(labelPanel, BorderLayout.WEST);
        JPanel textFieldPanel = new JPanel(new GridLayout(0,1));
        northPanel.add(textFieldPanel, BorderLayout.CENTER);

        labelPanel.add(new JLabel("From: ", SwingConstants.RIGHT));
        fromTF = new JTextField(25);
        textFieldPanel.add(fromTF);

        labelPanel.add(new JLabel("To: ", SwingConstants.RIGHT));
        toTF = new JTextField(25);
        textFieldPanel.add(toTF);

        labelPanel.add(new JLabel("CC: ", SwingConstants.RIGHT));
        ccTF = new JTextField(25);
        textFieldPanel.add(ccTF);

        labelPanel.add(new JLabel("BCC: ", SwingConstants.RIGHT));
        bccTF = new JTextField(25);
        textFieldPanel.add(bccTF);

        labelPanel.add(new JLabel("Subject: ", SwingConstants.RIGHT));
        subTF = new JTextField(25);
        textFieldPanel.add(subTF);

        labelPanel.add(new JLabel("Attachments: ", SwingConstants.RIGHT), BorderLayout.WEST);
        JPanel attPanel = new JPanel(new BorderLayout());
        textFieldPanel.add(attPanel);
        attachTF = new JTextField(25);
        attPanel.add(attachTF, BorderLayout.CENTER);
        browseB = new JButton("Browse");
        attPanel.add(browseB, BorderLayout.EAST);
        browseB.addActionListener(this);

        messageTA = new JTextArea(25, 50);
        messageTA.setLineWrap(true);
        getContentPane().add(new JScrollPane(messageTA), BorderLayout.CENTER);

        JPanel southPanel = new JPanel(new FlowLayout());
        getContentPane().add(southPanel, BorderLayout.SOUTH);
        sendB = new JButton("Send");
        southPanel.add(sendB);
        sendB.addActionListener(this);

        exitB = new JButton("Exit");
        southPanel.add(exitB);
        exitB.addActionListener(this);

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent evt)
                {
                    System.exit(0);
                }
            });
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource() == browseB)
        {
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
                return;
            File attachFile = fileChooser.getSelectedFile();
            attachTF.setText(attachTF.getText()+attachFile.getAbsolutePath()+",");
        }
        else if(evt.getSource() == sendB)
        {
            try
            {
                StringTokenizer strtok = new StringTokenizer(fromTF.getText()," \t\n,;");
                if(!strtok.hasMoreTokens())
                {
                    JOptionPane.showMessageDialog(this,
                        "Enter a valid from  email address ",
                        "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                EmailAddress from = new EmailAddress(strtok.nextToken(), null);

                Vector toList = new Vector();
                strtok = new StringTokenizer(toTF.getText(), " \t\n,;");
                while(strtok.hasMoreTokens())
                    toList.add(new EmailAddress(strtok.nextToken(),null));
                EmailAddress[] to = new EmailAddress[toList.size()];
                toList.toArray(to);

                toList = new Vector();
                strtok = new StringTokenizer(ccTF.getText(), " \t\n,;");
                while(strtok.hasMoreTokens())
                    toList.add(new EmailAddress(strtok.nextToken(),null));
                EmailAddress[] cc = new EmailAddress[toList.size()];
                toList.toArray(cc);

                toList = new Vector();
                strtok = new StringTokenizer(bccTF.getText(), " \t\n,;");
                while(strtok.hasMoreTokens())
                    toList.add(new EmailAddress(strtok.nextToken(),null));
                EmailAddress[] bcc = new EmailAddress[toList.size()];
                toList.toArray(bcc);

                Vector fileList = new Vector();
                strtok = new StringTokenizer(attachTF.getText(), "\n,;");
                while(strtok.hasMoreTokens())
                    fileList.add(new File(strtok.nextToken()));
                File[] attFiles = new File[fileList.size()];
                fileList.toArray(attFiles);

                Message msg = new Message(from, from, from, to, cc, bcc,
                    subTF.getText(), messageTA.getText(), "US-ASCII", attFiles);
                
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

                SMTPMailer.sendMail(msg);

                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

                JOptionPane.showMessageDialog(this,
                    "Message successfully sent.",
                    "Sent", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception exp)
            {
                exp.printStackTrace();
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                JOptionPane.showMessageDialog(this,
                    "Error sending Mail: "+exp.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        else if(evt.getSource() == exitB)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)
    {
        new MailCompDemo();
    }
}
