import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.moyosoft.connector.com.*;
import com.moyosoft.connector.exception.*;
import com.moyosoft.connector.ms.outlook.*;
import com.moyosoft.connector.ms.outlook.mail.*;
import com.moyosoft.connector.ms.outlook.util.*;
public class SendMail extends JFrame
{
private JButton buttonSend = new JButton("Send");
private JCheckBox checkBoxTurnOffSecurityWarnings = new JCheckBox(
"Turn OFF security warnings");
private JTextField textFieldTo = new JTextField();
private JTextField textFieldCc = new JTextField();
private JTextField textFieldBcc = new JTextField();
private JTextField textFieldSubject = new JTextField();
private JTextArea textAreaBody = new JTextArea();
public SendMail()
{
super("Send Mail");
init();
}
protected void init()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
dispose();
};
});
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonPanel.add(buttonSend);
buttonPanel.add(checkBoxTurnOffSecurityWarnings);
JPanel fieldsPanel = new JPanel(new GridBagLayout());
fieldsPanel.setBorder(BorderFactory.createEmptyBorder(8, 4, 12, 4));
GridBagConstraints c = new GridBagConstraints();
Insets leftInsets = new Insets(0, 3, 0, 10);
Insets rightInsets = new Insets(0, 10, 0, 3);
c.anchor = GridBagConstraints.NORTHWEST;
// 0,0
c.gridx = 0;
c.gridy = 0;
c.insets = leftInsets;
fieldsPanel.add(new JLabel("To:"), c);
// 0,1
c.gridx = 0;
c.gridy = 1;
c.insets = leftInsets;
fieldsPanel.add(new JLabel("Cc:"), c);
// 0,2
c.gridx = 0;
c.gridy = 2;
c.insets = leftInsets;
fieldsPanel.add(new JLabel("Bcc:"), c);
// 0,3
c.gridx = 0;
c.gridy = 3;
c.insets = leftInsets;
fieldsPanel.add(new JLabel("Subject:"), c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
// 1,0
c.gridx = 1;
c.gridy = 0;
c.insets = rightInsets;
fieldsPanel.add(textFieldTo, c);
// 1,1
c.gridx = 1;
c.gridy = 1;
c.insets = rightInsets;
fieldsPanel.add(textFieldCc, c);
// 1,2
c.gridx = 1;
c.gridy = 2;
c.insets = rightInsets;
fieldsPanel.add(textFieldBcc, c);
// 1,3
c.gridx = 1;
c.gridy = 3;
c.insets = rightInsets;
fieldsPanel.add(textFieldSubject, c);
JPanel headerPanel = new JPanel(new BorderLayout());
headerPanel.add(buttonPanel, BorderLayout.NORTH);
headerPanel.add(fieldsPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(textAreaBody), BorderLayout.CENTER);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
setSize(580, 400);
centerOnScreen(this);
buttonSend.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendMail();
}
});
}
private static void centerOnScreen(Window window)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = window.getSize();
windowSize.height = Math.min(windowSize.height + 20, screenSize.height);
windowSize.width = Math.min(windowSize.width, screenSize.width);
window.setLocation((screenSize.width - windowSize.width) / 2,
(screenSize.height - windowSize.height) / 2);
}
private void sendMail()
{
try
{
Outlook outlook = new Outlook();
OutlookSecurityManager securityManager = new OutlookSecurityManager(
outlook);
try
{
OutlookMail mail = new OutlookMail(outlook);
mail.setTo(textFieldTo.getText());
mail.setCC(textFieldCc.getText());
mail.setBCC(textFieldBcc.getText());
mail.setSubject(textFieldSubject.getText());
mail.setBody(textAreaBody.getText());
if(checkBoxTurnOffSecurityWarnings.isSelected())
{
securityManager.setSecurityWarningsEnabled(false);
}
mail.send();
if(checkBoxTurnOffSecurityWarnings.isSelected())
{
securityManager.setSecurityWarningsEnabled(true);
}
}
finally
{
outlook.dispose();
}
JOptionPane.showMessageDialog(this, "Message sent successfully");
}
catch(ComponentObjectModelException ex)
{
System.out.println("COM error has occured: ");
ex.printStackTrace();
JOptionPane.showMessageDialog(this, ex.getMessage());
}
catch(LibraryNotFoundException ex)
{
// If this error occurs, verify the file 'moyocore.dll' is present
// in java.library.path
System.out.println("The Java Outlook Library hasn't been found.");
ex.printStackTrace();
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
public static void main(String[] args)
{
// Set the look and feel
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
new SendMail().show();
}
}