Sending emails from your Java app via Gmail. Solve authorization problems.
It is very easy to send an email from your Java application with using javax.mail API. The problems are when we would like to use Gmail as a SMTP provider. In this tutorial I will show you how to glue javax.mail with Gmail to work well together.
Warning! After some time I came to the conclusion that this solution works without any limitation only if you use an app in the same location where you sign in to the gmail account. In other case, even when you unlock “unknown device” it works only for short period of time. After some time problem come back like a boomerang. In that case you should use Gmail API.
Build your app
You can use a sample project’s code in your app. It is already configured out of the box Sender class. It allows you to send email messages formatted in HTML.
Configure Gmail
First of all you have to turn on the access for less secure apps:

Then you can try to send a message. It is very likely that Gmail will block sender and will treat it as an impostor.
In that case you will see in Recently used devices:

Open the Unknown device row:

Click Notification & alerts page.

Then you see the list of recent security events. Let’s find Google prevented a sign-in from a less secure app row, click it.

Select Unknown device.

Now you can confirm that Yes, That was me.
Within around 15 minutes from that moment your Java application should be able to send emails.
More on google support page.
If you have any questions or comments. Please let me know. Constructive feedback is appreciated.