Java Code to Send E-mail using Gmail SMTP Server

This is the simple program that uses the Java Mail API to send the email from the Java code using the SMTP server of the Gmail.

In this simple program, we are going to use the Java Mail API by Oracle. You can download the Java Mail API Jar files directly from the links given below, or you can also download from the official site.

Where can you use this code snippet?

If in your application you ever need to send the email to the user for confirmation of registration, password reset links and any email then this simple program is very handy for you.

If you don’t want to setup your mail server and want to send the email to your user or for testing purpose or want to set up things quickly, then you can use the SMTP server of the email.

I used this program to send an email via Gmail server in my college project for password reset, confirmation, email notification.

Source Code of Sending Mail in Java via Gmail SMTP server


While running this program you can hit by the AuthenticationFailedException. To resolve this exception you have to Turn on Access to less secure apps in Google account settings.

You can find this setting page on this link:

https://myaccount.google.com/lesssecureapps

In this page Turn On the Feature then you can send the Email through your Java code via Gmail.

If you like this program and want to know about more programming code snippets then you can also subscribe to our new letter.

2 replies

Trackbacks & Pingbacks

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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