Skip to content

JavaMail API – Sending Email using Java from Gmail SMTP

This tutorial will help you for Sending Email using Java from Remote SMTP Server like Gmail. This tutorial using javax.mail (JavaMail) API for sending email

Steps
3
Category
Networking
Covers
Email, Java
Versions & support
About Java →
On this page 3 steps

JavaMail API supports the JavaMail (javax.mail) interface for sending email messages. It provides classes for sending email from remote smtp server with authentication like Gmail, sendgrid etc.

In this article we are using JavaMail API for sending emails using Java programming language through remote smtp server. This articles example are using Gmail smtp server as a remote smtp server for sending emails.

Step 1 of 3Setup JavaMail Environment

First we need to download jar file containing all classes in javax.mail. Download jar (mail.jar) file from oracle official website.

Now set the classpath in system environment. Windows users make sure you have configured PATH variable for your Java installation.

Windows:

c:> set classpath=mail.jar;.;

Linux:

# export JAVA_HOME=/opt/jdk1.8.0_05/
# export PATH=$PATH:$JAVA_HOME/bin
# export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar:mail.jar:.

Step 2 of 3Write a Java Program to Send Email

Create a java file SendMail.java with the following content. In this script you need to change many setting as per your setup. This script can be used for sending email through TLS or SSL SMTP connection through Gmail smtp servers.

Step 3 of 3Execute Program to Send Email

Finally we need to execute java program to send email. as we know that this happens in two steps, first to compile program and second to run it.

# javac SendMail.java
# java SendMail

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

2 Comments

  1. This post is great. I checked out your blog site pretty regularly, and you’re
    always coming up with some great staff. I shared this post on my
    Twitter, and my followers loved it! To the
    next. Cheers.

  2. sir getting this exception !!
    plz help
    run:
    Exception in thread “main” java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.(Session.java:212)
    at javax.mail.Session.getInstance(Session.java:248)
    at testing.SendMail.main(SendMail.java:41)
    Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 4 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top