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



How to set look and feel on Java Swing applications

Use the method setLookAndFeel like the following to change Java Swing applications’ appearance

try {
} catch (Exception ex) {
//Just print stacktrace here since it's an example.

And if you get an exception like the following one while trying to integrate SyntheticaStandardLookAndFeel for your application;

java.text.ParseException: Error parsing: org.xml.sax.SAXException: bind: unknown type REGİON null
    at javax.swing.plaf.synth.SynthParser.parse(
    at javax.swing.plaf.synth.SynthLookAndFeel.load(
    at de.javasoft.plaf.synthetica.SyntheticaLookAndFeel.loadXMLConfig(
    at de.javasoft.plaf.synthetica.SyntheticaLookAndFeel.<init>(
    at de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel.<init>(
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at java.lang.Class.newInstance0(
    at java.lang.Class.newInstance(
    at javax.swing.UIManager.setLookAndFeel(
    at examples.WelcomeForm.main(

You should change your current locale to English before setting Look and Feel like the code below;

try {
} catch (Exception ex) {
//Just print stacktrace here since it's an example.

example Look and Feel class names;