Skip to content

Sending or receiving emails using OAuth2 in Java for Microsoft Office 365 or Outlook

Notifications You must be signed in to change notification settings

zahangirbd/JavaMailUsingOAuthForOffice365

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaMailUsingOAuthForOffice365 · GitHub license

Send and read emails using OAuth2 in Java for Microsoft Office365 or Outlook

Objectives

A very few resources with a complete set of MS configurations and a sample project are available online to send and/or read emails through Microsoft Office365 or Outlook using OAuth2 in Java. Thus, the primary targets are:

  • to provide the guidelines for the MS configurations for sending emails through Microsoft Office365 or Outlook in Java using OAuth2 (i.e., using SMTP AUTH command)
  • to list down the the MS configurations for reading emails from Microsoft Office365 or Outlook in Java using OAuth2

Pre-requisites: Setup Email SMTP Settings for Office365 with OAuth2 Authentication

  1. A step by step setup guideline is available here.

  2. Once we complete the setup, we shall copy the following items from Azure Portal

    - Application (Client) ID
    - Directory (Tenant) ID

Setup the Java project

  1. We can setup this project by any of the following two options

    • Import this java project into IDE (e.g., eclipse) as a gradle project
    • Prepare this java project for eclipse by following command and then open the project into IDE
       gradle eclipse
  2. Change the following configurations into application.properties under src/resources We will need Application (Client) ID, Directory (Tenant) ID, redirectUrl, sender email address and receiver email addresses here

    clinetId=<YOUR_REGISTERED_APP_CLIENT_ID>
    userName=<YOUR_OFFICE365_EMAIL_ADDRESS>
    authEndpointUrl=https://login.microsoftonline.com/<YOUR_TENANT_ID>/oauth2/v2.0/authorize
    scopes=offline_access,openId,https://outlook.office.com/SMTP.Send
    redirectUrl=http://localhost
    toAddresses=<TO_EMAIL_ADDRESSES>
  3. Run the following Java class file

    	org.zahangirbd.email.EmailSender
    • It will collect accessToken of OAuth2 using default browser with the given redirect URL
    • It will use the accessToken for SMTP AUTH for sending email
    • It will send an test email to the provide toAddresses mentioned in application.properties

About

Sending or receiving emails using OAuth2 in Java for Microsoft Office 365 or Outlook

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages