This tutorial will walk you through the steps of building a Sending Email Example with Gmail and Java Mail API.

What you'll need

  • JDK 1.7+
  • Maven 3+
  • OAuth Access Token of your Google Account. Check out more details on How to get the access token at OAuth2DotPyRunThrough

Stack

  • Java
  • Java Mail API
  • OAuth 2 Authorization Flow
  • SMTP Server
  • Sending Email

Init project structure and dependencies

Project structure

├── src
│   └── main
│       └── java
│           └── com
│               └── hellokoding
│                   └── mail
│                       ├── Main.java
│                       └── SendingMailThroughGmailSMTPServer.java
└── pom.xml

Project dependencies


Sending Mail with Java Mail API and Gmail SMTP Server


Run


Replace below macros with your respective values and run the command

mvn clean package exec:java -Dexec.args="{FROM_USER_NAME} {FROM_USER_FULLNAME} {FROM_USER_ACCESSTOKEN} {TO_USER_EMAIL}"  

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/java-examples/javamail-gmail