Eclipse Java

Send email using Java Application

It is a beginners tutorial, It has each and every basic step needed for this project.

First One: Creating a Maven Project

I have created Maven project, because we don’t need to download manually the dependencies and put file in dependencies folder.

In order to create a Maven project go to:

File -> New -> Other -> Maven Project

After selecting the Maven project, click Next.

Must check “Create a simple project”, then click next.

Then write any unique Group id, Artifact Id, Name of Project, and click Finish.

Step Two: Add dependencies

Open Pom.xml and add dependencies on it.

In order to add dependencies, go to and search required dependencies.

In this project, I am using default Java mail, which is com.sun.mail. Click on it.

Different versions will appear, select the latest one. Click on it.

Now click on Maven and copy dependency.

And paste it into pom.xml. Don’t forget to add it inside <dependencies> </dependencies>

You can copy form below as well.

		<!-- -->

Step Three: Add your code.

For it first, create a class. By going to New -> Class

Give the project a name, eg. EmailUsingGmail

Now check out the complete code.

Don’t forget to add UserName and Password:

package sendemail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailUsingGmail {

    public static void main(String[] args) {

        final String username = "";
        final String password = "password";

        Properties prop = new Properties();
		prop.put("", "");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(""));
            message.setSubject("Testing Gmail TLS");
            message.setText("Dear Mail Crawler,"
                    + "\n\n Please do not spam my email!");



        } catch (MessagingException e) {


Step Four: Run the project

In order to Run the project, go to Run As -> Run Configuration

Now browse the Project

Select your project name

Now select the main class, for it, click on Search.. button

And select the main class

Now click on Run


Step Five: Export the Project

Right-click on the Project, and click on Export

If you don’t want to add manually the classpath while running the application, select Runnable JAR file and click next

Then select the class name – with project name from dropdown menu

Give jar file a name, and give it folder destination where ever you want it to save. And click on save

In order to Run the jar, open command prompt, then browse the folder where you have saved the jar using “cd” command

Now Run the jar using the command: java -jar filename.jar

How to run a jar file, using command prompt

Go to the project, open Maven Dependencies, and copy the dependencies jar loactions

Open the project check the project name and class name

Command to run jar file is : C:>java -classpath jarname.jar;depencies-jar1.jar;dependencies-jar2.jar packagename.subpackage-name.classname

In this project command will be:

C:\Users\Abhay\Desktop>java -classpath EmailUsingGmail-notRunnablejar.jar;C:\Users\Abhay\.m2\repository\com\sun\mail\javax.mail\1.6.2\javax.mail-1.6.2.jar;C:\Users\Abhay\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar  sendemail.EmailUsingGmail

Now we are good to go.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.