Email Application in Java

Email Application in Java

Introduction

In this tutorial, we will learn how to create a simple Email Application in Java with Swing and the JavaMail API. Email is an essential part of communication, allowing us to easily send messages and documents to people all over the world.

The JavaMail API is a library that provides a platform-independent and protocol-independent framework to build email applications in Java. We will use this API to send emails from our application. The sender will be able to send a message and any type of file as an attachment to the receiver. Let’s get started!

Email Application in Java: Project Overview

Project Name:Email Application in Java
Abstract:It’s a GUI-based project used with the Swing module to manage Email Application in Java using JavaMail API.
Language:Java
IDE:NetBeans IDE
Java version
(Recommended):
Java SE 18.0. 2.1
Database:None
Type:Desktop Application
Recommended for:Beginners of Java
Time Needed:1-2 hours

Setup the development environment

  • Open NetBeans IDE and create a new Java project with the name SendMail. Now create a new package with the name sendmail, inside this package, we will create the Java file.
  • Download javax.mail jar and extract the zip file to your download folder. We need to add this jar file to the project as our project depends on this.
  • Expand the project, right-click on libraries and click on add jar/folder. Now browse the two jar file named javax.mail.jar and activation.jar and add them here.

The project structure should look like this:

project folder

Complete Code for Email Application in Java

Create a file in the sendmail package with the name Mail.java. In this file inside the sendToMail function, you need to change the username and password according to your sender email credentials. 

NOTE: By default, google does not allow any less secure third-party application to sign in to your Google account. For this, you need to create an App Password in your Google account admin panel. And use this App password instead of your Gmail account password in the sendToMail function of our project. Refer to this for help.

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.io.File;
import java.util.Properties;
import javax.swing.JFileChooser;


public class Mail extends javax.swing.JFrame {

    public Mail() {
        initComponents();
    }
    JFileChooser fileChooser = new JFileChooser();

  
    @SuppressWarnings("unchecked")
    private void initComponents() {

        Body = new javax.swing.JPanel();
        SendTo = new javax.swing.JTextField();
        Subject = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        MessageArea = new javax.swing.JTextArea();
        DirectoryPath = new javax.swing.JTextField();
        Attachment = new javax.swing.JButton();
        Send = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Body.setBackground(new java.awt.Color(73, 197, 182));

        jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 15));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Send To:");

        jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 15));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Subject:");

        MessageArea.setColumns(20);
        MessageArea.setRows(5);
        MessageArea.setOpaque(false);
        jScrollPane1.setViewportView(MessageArea);

        Attachment.setBackground(new java.awt.Color(0, 0, 0));
        Attachment.setFont(new java.awt.Font("Segoe UI", 1, 14));
        Attachment.setForeground(new java.awt.Color(255, 255, 255));
        Attachment.setText("Attachment");
        Attachment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AttachmentActionPerformed(evt);
            }
        });

        Send.setBackground(new java.awt.Color(0, 0, 0));
        Send.setFont(new java.awt.Font("MS UI Gothic", 1, 14));
        Send.setForeground(new java.awt.Color(255, 255, 255));
        Send.setText("Send");
        Send.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        Send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendActionPerformed(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(0, 0, 0));
        jButton2.setFont(new java.awt.Font("MS UI Gothic", 1, 14));
        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setText("Cancel");
        jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setBackground(new java.awt.Color(0, 0, 0));
        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 3, 24));
        jLabel3.setText("Send Mail");

        javax.swing.GroupLayout BodyLayout = new javax.swing.GroupLayout(Body);
        Body.setLayout(BodyLayout);
        BodyLayout.setHorizontalGroup(
            BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BodyLayout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(Send)
                .addGap(40, 40, 40)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BodyLayout.createSequentialGroup()
                .addContainerGap(112, Short.MAX_VALUE)
                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BodyLayout.createSequentialGroup()
                        .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(BodyLayout.createSequentialGroup()
                                .addComponent(Attachment, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(DirectoryPath))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, BodyLayout.createSequentialGroup()
                                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Subject)
                                    .addComponent(SendTo))))
                        .addGap(113, 113, 113))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BodyLayout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(180, 180, 180))))
        );
        BodyLayout.setVerticalGroup(
            BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(BodyLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendTo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Subject, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(Attachment, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(DirectoryPath))
                .addGap(19, 19, 19)
                .addGroup(BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Send, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(Body, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Body, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }                        

    private void AttachmentActionPerformed(java.awt.event.ActionEvent evt) {                                           
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));

        int result = fileChooser.showOpenDialog(this);

        if (result == JFileChooser.APPROVE_OPTION) 
        {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            DirectoryPath.setText(selectedFile.getAbsolutePath());
        }          
    }                                          

    private void SendActionPerformed(java.awt.event.ActionEvent evt) {                                     
        Boolean sendTo =!SendTo.getText().equals("");
        Boolean subject = !Subject.getText().equals("");  
        Boolean directoryPath = !DirectoryPath.getText().equals("");
        Boolean messageArea =!MessageArea.getText().equals("");

            if (sendTo && subject && directoryPath && messageArea )
            {
            SendToEmail();
            }
            else 
            {
              System.out.print("Complete the blank textfield or text area..");

            }
        
    }                                    
    public void SendToEmail(){

        final String to = SendTo.getText();

         // Sender's email ID needs to be mentioned
        final String from = ""; //change according to your email: 

           //Embedded account
          String username = "";//change according to your email
          String password = "";//change with your App Password created in the google account for less secure app

          //Server and hosting 
         Properties props = new Properties();
          props.put("mail.smtp.host", "smtp.gmail.com");
           props.put("mail.smtp.port", "587");	
           props.put("mail.smtp.auth", "true");
           props.put("mail.smtp.starttls.enable", "true");

         // Get the Session object.
         Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
               protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
               }
            });

         try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO,
               InternetAddress.parse(to));

            // Set Subject: header field
            message.setSubject(Subject.getText());

            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Now set the actual message via getText
            messageBodyPart.setText(MessageArea.getText());

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part via getText
            multipart.addBodyPart(messageBodyPart);

            //get your file and executed 
            messageBodyPart = new MimeBodyPart();
             File selectedFile = fileChooser.getSelectedFile();

            String fileChooser = selectedFile.getAbsolutePath();
            DataSource source = new FileDataSource(fileChooser);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileChooser);
            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            message.setContent(multipart);

            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");

         } catch (MessagingException e) {
            throw new RuntimeException(e);
         }
        
        
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //ClearingValue
        
        SendTo.setText("");
        Subject.setText("");
        DirectoryPath.setText("");
        MessageArea.setText("");
        
    }                                        

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Mail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Mail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Mail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Mail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Mail().setVisible(true);
            }
        });
    }

    private javax.swing.JButton Attachment;
    private javax.swing.JPanel Body;
    private javax.swing.JTextField DirectoryPath;
    private javax.swing.JTextArea MessageArea;
    private javax.swing.JButton Send;
    private javax.swing.JTextField SendTo;
    private javax.swing.JTextField Subject;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
}

Output for Email Application in Java

Image Output:

output

Video Output:

Thank you for visiting our website.


Also Read:

Share:

Author: Ayush Purawr