This post walks you through the steps of Sending Email Through Gmail SMTP Server with Java Mail API and OAuth 2 Authorization.

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

Project structure

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

Project dependencies


Sending Mail


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

[email protected]:hellokoding/sendingmail-gmailsmtpserver-javamailapi.git

https://github.com/hellokoding/sendingmail-gmailsmtpserver-javamailapi