How do I create a simple mail client program in Swing?

The code snippet below show you how to create a simple Java Swing application that can be used to send an e-mail. The program allows user to supply the from email address, to email address, the subject and the message of the email. User need to select the available SMTP server to connect to and provide the username and password for authentication to the mail server.

Here is the user interface of this simple email client:

Simple E-mail Client

Simple E-mail Client

The main routine for sending the email is in the SendEmailActionListener class, which is an implementation of an ActionListener interface that will handle email sending process when Send E-mail button is pressed.

package org.kodejava.mail;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

public class SendEmailClient extends JFrame {
    private final JTextField fromField = new JTextField();
    private final JTextField toField = new JTextField();
    private final JTextField subjectField = new JTextField();
    private final JComboBox<String> mailSmtpHostComboBox = new JComboBox<>();
    private final JTextField usernameField = new JTextField();
    private final JPasswordField passwordField = new JPasswordField();
    private final JTextArea contentTextArea = new JTextArea();

    private SendEmailClient() {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SendEmailClient client = new SendEmailClient();

    private void InitializeUI() {
        setTitle("Send E-mail Client");
        setSize(new Dimension(500, 500));

        getContentPane().setLayout(new BorderLayout());

        // Header Panel
        JPanel headerPanel = new JPanel();
        headerPanel.setLayout(new GridLayout(6, 2));
        headerPanel.add(new JLabel("From:"));

        headerPanel.add(new JLabel("To:"));

        headerPanel.add(new JLabel("Subject:"));

        headerPanel.add(new JLabel("SMTP Server:"));

        headerPanel.add(new JLabel("Username:"));

        headerPanel.add(new JLabel("Password:"));

        // Body Panel
        JPanel bodyPanel = new JPanel();
        bodyPanel.setLayout(new BorderLayout());
        bodyPanel.add(new JLabel("Message:"), BorderLayout.NORTH);
        bodyPanel.add(contentTextArea, BorderLayout.CENTER);

        JPanel footerPanel = new JPanel();
        footerPanel.setLayout(new BorderLayout());
        JButton sendMailButton = new JButton("Send E-mail");
        sendMailButton.addActionListener(new SendEmailActionListener());

        footerPanel.add(sendMailButton, BorderLayout.SOUTH);

        getContentPane().add(headerPanel, BorderLayout.NORTH);
        getContentPane().add(bodyPanel, BorderLayout.CENTER);
        getContentPane().add(footerPanel, BorderLayout.SOUTH);

    private class SendEmailActionListener implements ActionListener {
        SendEmailActionListener() {

        public void actionPerformed(ActionEvent e) {
            Properties props = new Properties();
            props.put("", mailSmtpHostComboBox.getSelectedItem());
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "");
            props.put("mail.smtp.ssl.protocols", "TLSv1.2");

            Session session = Session.getDefaultInstance(props);
            try {
                InternetAddress fromAddress = new InternetAddress(fromField.getText());
                InternetAddress toAddress = new InternetAddress(toField.getText());

                Message message = new MimeMessage(session);
                message.setRecipient(Message.RecipientType.TO, toAddress);

                Transport.send(message, usernameField.getText(),
                        new String(passwordField.getPassword()));
            } catch (MessagingException ex) {

Maven Dependencies


Maven Central Maven Central



  1. Please help with this error:
    The error message in Transport.send() is: No suitable method found for Transport.send(Message, string, string). There is another method Transport.send(message, addresses) in the javax.mail.Transport but that does not match the number of parameters.

  2. Now I’m using javax.mail-api-1.5.2.jar. No compilation error. But when running getting exception:

    Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
        at javax.mail.Session.initLogger(
        at javax.mail.Session.(
        at javax.mail.Session.getDefaultInstance(
        at javax.mail.Session.getDefaultInstance(
  3. Hi Bro 🙂

    First of all, thank you.

    Second, I have a problem.

    Transport.send(message, usernameField.getText(), new String(passwordField.getPassword()));

    Trasport.send() isn’t working.


    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: The method send(Message, Address[]) in the type Transport is not applicable for the arguments (Message, String, String)
    at mail.SendEmailClient$SendEmailActionListener.actionPerformed(
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  4. Hello,

    The Transport.send(Message msg, String user, String password) was added in javax.mail-api-1.5.jar. So in addition to mail-1.4.7.jar, you also need to use the javax.mail-api-1.5.x.jar. In my case I use the 1.5.6 version of javax.mail-api.

  5. Hi Shruthi,

    You can create a simple Maven application and add the dependencies listed above to your Maven pom.xml file. If you don’t use Maven, just download the jar from the link provided above and add it to your project library in your IDE.

  6. Hi, if it is possible, can you help me about my project. I create email system in Java SE GUI. I save all mails in messageList. And I have 2 lists. One of them is inbox, the other one is sentbox. But when I send a message, who is receiver see the message if him/her outbox. But it have to be in inbox. How can I fix it?

  7. javax.mail.SendFailedException: Sending failed;
      nested exception is:
        class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. b78sm12619881pfb.144 - gsmtp

    How can I solve this?

  8. Hi, just wondering if this still works with gmail? Knowing google’s anti-competitive behavior in barring other mail clients (like KMail) I wouldn’t be surprised if it didn’t. If that’s correct, you might wanna update the tutorial on how to enable “legacy” access to gmail accounts. Cheers!


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.