Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application for the WebBuy Company that allows a user to compose the th

ID: 664863 • Letter: W

Question

Write an application for the WebBuy Company that allows a user to compose the three parts of a complete e-mail message: the "To:", "Subject:", and "Message:" text. The "To:" and "Subject:" text areas should provide a single line for data entry. The "Message:" area should allow multiple lines of input and be able to scroll if necessary to accomodate a long message. The user clicks a button to send the e-mail message. When the message is complete and the Send button is clicked, the application displays "Mail has been sent!" on a new line in the message area. Save the file as JEmail.java.

Explanation / Answer

JEmail.java

package com.tutai;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class JEmail {
   public static void sendTheEmail(Properties smtpProp, String to,
            String subject, String message)
            throws MessagingException, AddressException, IOException {

        final String uName = smtpProp.getProperty("mail.user");
        final String pass = smtpProp.getProperty("mail.password");
       
      
        Authenticator authentication = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(uName, pass);
            }
        };
        Session session = Session.getInstance(smtpProp, authentication);
      
        Message msge = new MimeMessage(session);

        msge.setFrom(new InternetAddress(uName));
        InternetAddress[] toes = { new InternetAddress(to) };
        msge.setRecipients(Message.RecipientType.TO, toes);
        msge.setSubject(subject);
        msge.setSentDate(new Date());
      
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(message, "text/html");
      
        Multipart mltipart = new MimeMultipart();
        mltipart.addBodyPart(messageBodyPart);
       
        msge.setContent(mltipart);

        Transport.send(msge);

    }
}

ConfigurationUtility.java

package com.tutai;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class ConfigurationUtility {
   private File configurationFile=new File("smtp.properties");
    private Properties configrationProps;
   
    public Properties loadProps() throws IOException {
        Properties dfltProperties = new Properties();
        // sets default properties
        dfltProperties.setProperty("mail.smtp.host","smtp.gmail.com");
        dfltProperties.setProperty("mail.smtp.port","587");
        dfltProperties.setProperty("mail.user","<username>@gmail.com");
        dfltProperties.setProperty("mail.password","<password>");
        dfltProperties.setProperty("mail.smtp.starttls.enable","true");
        dfltProperties.setProperty("mail.smtp.auth","true");
       
        configrationProps = new Properties(dfltProperties);
       
        // loads properties from file
        if (configurationFile.exists()) {
            InputStream inputStream = new FileInputStream(configurationFile);
            configrationProps.load(inputStream);
            inputStream.close();
        }
       
        return configrationProps;
    }
   
    public void saveProperties(String host, String port, String user, String pass) throws IOException {
        configrationProps.setProperty("mail.smtp.host",host);
        configrationProps.setProperty("mail.smtp.port",port);
        configrationProps.setProperty("mail.user",user);
        configrationProps.setProperty("mail.password",pass);
        configrationProps.setProperty("mail.smtp.starttls.enable","true");
        configrationProps.setProperty("mail.smtp.auth","true");
       
        OutputStream optStream = new FileOutputStream(configurationFile);
        configrationProps.store(optStream, "host setttings");
        optStream.close();
    }
}


Note: Set your email username and password for the lines:-

dfltProperties.setProperty("mail.user","<username>@gmail.com");
dfltProperties.setProperty("mail.password","<password>");

EmailSender.java

package com.tutai;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class EmailSender extends JFrame {
    private ConfigurationUtility configUtil = new ConfigurationUtility();
      
    private JLabel lblTo = new JLabel("To: ");
    private JLabel lblSubject = new JLabel("Subject: ");
   
    private JTextField fldTo = new JTextField(30);
    private JTextField fldSubject = new JTextField(30);
   
    private JButton btnSend = new JButton("SEND");
   
    private JTextArea txtAreaMessage = new JTextArea(10,30);
   
    private GridBagConstraints cnstrnts = new GridBagConstraints();
   
    public EmailSender() {
        super("Send Email");
       
        setLayout(new GridBagLayout());
        cnstrnts.anchor = GridBagConstraints.WEST;
        cnstrnts.insets = new Insets(5,5,5,5);
   
        setupTheForm();
       
        pack();
        setLocationRelativeTo(null);  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }

   private void setupTheForm() {
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 0;
        add(lblTo, cnstrnts);
       
        cnstrnts.gridx = 1;
        cnstrnts.fill = GridBagConstraints.HORIZONTAL;
        add(fldTo, cnstrnts);
       
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 1;
        add(lblSubject, cnstrnts);
       
        cnstrnts.gridx = 1;
        cnstrnts.fill = GridBagConstraints.HORIZONTAL;
        add(fldSubject, cnstrnts);
       
        cnstrnts.gridx = 2;
        cnstrnts.gridy = 0;
        cnstrnts.gridheight = 2;
        cnstrnts.fill = GridBagConstraints.BOTH;
        btnSend.setFont(new Font("Arial",Font.BOLD,16));
        add(btnSend,cnstrnts);
       
        btnSend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                btnSendActionPerformed(event);
            }
        });
       
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 2;
        cnstrnts.gridheight = 1;
        cnstrnts.gridwidth = 3;
               
        cnstrnts.gridy = 3;
        cnstrnts.weightx = 1.0;
        cnstrnts.weighty = 1.0;
       
        add(new JScrollPane(txtAreaMessage), cnstrnts);  
    }
   
    private void btnSendActionPerformed(ActionEvent event) {
        if (!validateFields()) {
            return;
        }
       
        String toAddress = fldTo.getText();
        String subject = fldSubject.getText();
        String message = txtAreaMessage.getText();
              
        try {
            Properties smtpProperties = configUtil.loadProps();
            JEmail.sendTheEmail(smtpProperties, toAddress, subject, message);
           
            JOptionPane.showMessageDialog(this,
                    "The e-mail is sent successfully");
           
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,
                    "Error while sending the email" + ex.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
   
    private boolean validateFields() {
        if (fldTo.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Please enter the To address",
                    "Error", JOptionPane.ERROR_MESSAGE);
            fldTo.requestFocus();
            return false;
        }
       
        if (fldSubject.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Please enter the subject",
                    "Error", JOptionPane.ERROR_MESSAGE);
            fldSubject.requestFocus();
            return false;
        }
       
        if (txtAreaMessage.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Please enter the message",
                    "Error", JOptionPane.ERROR_MESSAGE);
            txtAreaMessage.requestFocus();
            return false;
        }
       
        return true;
    }
   
    public static void main(String[] args) {
      
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
       
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EmailSender().setVisible(true);
            }
        });
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote