How to send an e-mail using Java Mail API (Swing Desktop Application using Google’s SMTP Server)

Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

To use the Java Mail API inside your project, you should download the latest Java Mail API jar from the following link;

Java Mail API 1.4.4 Download

You should have a reference for the mail.jar inside the project and you can use the following source code for sending e-mail from Java Programs.

private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {

boolean isSent = true;

try {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", txtfrom.getText()); //such as
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("", txthostName.getText());   //use for Google
        properties.put("mail.smtp.user", txtfrom.getText()); //such as
        properties.put("mail.smtp.port", txtPort.getText()); //use 465 for Google's SMTP server
        properties.put("mail.smtp.socketFactory.port", txtPort.getText()); //use 465 for Google's SMTP server
        properties.put("mail.smtp.socketFactory.class", "");
        properties.put("mail.smtp.socketFactory.fallback", "false");

        Authenticator mailAuthenticator = new MailAuthenticator();
        Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator);
        Message message = new MimeMessage(mailSession);

        InternetAddress fromAddress = new InternetAddress(txtfrom.getText());
        InternetAddress toAddress = new InternetAddress(txtto.getText());

        message.setRecipient(RecipientType.TO, toAddress);
        //you can use RecipientTypes such as


} catch (Exception e) {
     lblInfo.setText("ERROR:" + e.getMessage());
     isSent = false;

     if(isSent == true) {
     lblInfo.setText("Your e-mail has been sent.");


private class MailAuthenticator extends javax.mail.Authenticator {

private PasswordAuthentication authentication = null;
private String username;
private String password;

public MailAuthenticator() {
this.username = txtusername.getText();
this.password = new String(txtPassword.getPassword());
this.authentication = new PasswordAuthentication(username,password);

protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;


The following program uses Authentication before sending e-mails.

The screenshot for the program;

Download Source of the Program (Swing Java Mail API example program)

After downloading the program, you should  click on the .jar file under the following directory