Net talk email Oauth2 Step by Step for outlook, Code for Gmail. Yahoo not working!

This is a document of how I created an Oauth2 account with office365. It may not be 100% accurate. Give it a try if you have never tried to send email using Oauth2.

Setup outlook for Oauth2 and email

Register at this page. You will need to provide a credit card.

https://azure.microsoft.com/en-us/get-started/welcome-to-azure/

Start here

https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade

New Registration

Give it a name

Select the account type ( I chose multitenant

Redirect URL i chose web and http://localhost:8123

Click Register

Note - You should now see the application ID You will need this for your application

Click on the menu item Certificate & Secrets or Click on Add a Certificate or Secret on the right

Click on New Client Secret

Add a description

Select an expiration

Click ADD

You will see a value and secret ID. IMPORTANT! You want the VALUE and not the SECRET ID! you will use the VALUE as the secret id in NetTalk.

Click on the menu item API and permissions

Click on Add a permission

Click on Microsoft Graph

Click on Delegated permissions

( note there is a search for permissions. you will check two permissions )

Check offline_access
Check SMTP.Send

Click Add permissions

You should see your permissions added. Click on SMTP.Send

That should be it on the MS side.

The code for the OAuthParams is as follows

  clear(OAuthParms)
  OAuthParms.pServiceName    = 'Microsoft'
  OAuthParms.pOAuthVersion   = 2
  OAuthParms.pClientId       = glo:ClientIdOutlook ! 
  OAuthParms.pClientSecret   = glo:ClientSecretOutlook !
  OAuthParms.pRedirectURL    = 'http://localhost:8123'
  OAuthParms.pExternalBrowser = netOAuth:UseLocalBrowser
  OAuthParms.pListenTLS      = false
  OAuthParms.pListenPort     = 8123
  OAuthParms.pListenBindIP   = '127.0.0.1'      
  OAuthParms.pScope          = 'https://outlook.office.com/SMTP.send offline_access'
  OAuthParms.pAuthorizeURL   = 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize'
  OAuthParms.pAccessTokenURL = 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token'
  
  OAuthParms.pExpectedCertificateCommonName = 'stamp2.login.microsoftonline.com'    

Sending the email works as follows:

  EmailTo = glo:ToEmail
  EmailSubject = glo:ToEmailSubject
  EmailMessageText = glo:ToEmailMessageText  
  
  EmailFrom = glo:FromEmailOutlook
  EmailUser = glo:FromEmailOutlook      
  EmailServer          = 'smtp.office365.com'
  EmailPort            = 587 
  EmailPassword        = ''
  EmailToken           = OAuthParms.rToken
  EmailSSL             = TRUE
  EmailStartTLS        = TRUE

I would encourage anyone to try this out and please improve the documents where possible.

I also go gmail to work but I do not have the instructions for how to setup the gmail with google.

I can provide the code section.

Gmail OAuthParams:

  clear(OAuthParms)
  OAuthParms.pServiceName    = 'Google'
  OAuthParms.pOAuthVersion   = 2
  OAuthParms.pClientId       = glo:ClientIdGmail !Marathon 
  OAuthParms.pClientSecret   = glo:ClientSecretGmail !Marathon
  OAuthParms.pRedirectURL    = 'http://localhost:8123'
  OAuthParms.pExternalBrowser = netOAuth:UseLocalBrowser
  OAuthParms.pListenTLS      = false
  OAuthParms.pListenPort     = 8123
  OAuthParms.pListenBindIP   = '127.0.0.1'
  OAuthParms.pScope          = 'https://mail.google.com/'  
  OAuthParms.pAuthorizeURL   = 'https://accounts.google.com/o/oauth2/auth'
  OAuthParms.pAccessTokenURL = 'https://accounts.google.com/o/oauth2/token'

Gmail Email:

  EmailTo = glo:ToEmail
  EmailSubject = glo:ToEmailSubject
  EmailMessageText = glo:ToEmailMessageText  
  
  EmailFrom = glo:FromEmailGmail
  EmailUser = glo:FromEmailGmail      
  EmailServer          = 'smtp.gmail.com'
  EmailPort            = 587 
  EmailPassword        = ''
  EmailToken           = OAuthParms.rToken
  EmailSSL             = TRUE
  EmailStartTLS        = TRUE

I failed to get Yahoo to work. If anyone can help with this I would be very grateful.

Attempted OAuthParams:

  clear(OAuthParms)
  OAuthParms.pServiceName    = 'Yahoo'
  OAuthParms.pOAuthVersion   = 2
  OAuthParms.pClientId       = glo:ClientIDYahoo   
  OAuthParms.pClientSecret   = glo:ClientSecretYahoo
  OAuthParms.pRedirectURL    = 'https://falcon.mscflexone.com'
  OAuthParms.pExternalBrowser = netOAuth:UseLocalBrowser
  OAuthParms.pListenTLS      = false
  OAuthParms.pListenPort     = 8123
  OAuthParms.pListenPort     = 80
  OAuthParms.pListenBindIP   = '127.0.0.1'
  OAuthParms.pScope          = 'email'
  OAuthParms.pAuthorizeURL   = 'https://api.login.yahoo.com/oauth2/request_auth'
  OAuthParms.pAccessTokenURL = 'https://api.login.yahoo.com/oauth2/get_token'
  
  OAuthParms.pExternalBrowser = netOAuth:OnScreenHTML 

Email:

  EmailTo = glo:ToEmail
  EmailSubject = glo:ToEmailSubject
  EmailMessageText = glo:ToEmailMessageText  
  
  EmailFrom = glo:FromEmailYahoo
  EmailUser = glo:FromEmailYahoo      
  EmailServer          = 'smtp.mail.yahoo.com'
  EmailPort            = 587 
  EmailPassword        = ''
  EmailToken           = OAuthParms.rToken
  EmailSSL             = TRUE
  EmailStartTLS        = TRUE     

Jeff Hojka

2 Likes

These are the steps I tried to get yahoo email to work

Create an account at Yahoo

Go to the following link to create an app

Create Application

Give it a name and description

Homepage URL is optional

Redirect URL may not be local host

Chose Confidential Client

Check OpenID Connect Permissions

Select Email

Click Create App

Use the client ID and Client Secret in your application

1 Like

Hi Jeff: Many thanks for posting your code. It’s very helpful!

When using it with O365, does the Sent email show up in the Sender’s Outlook account?

Also, when I set up the Oauth account under the company, can it then be used by the individual employees (vs. having to set up an account for each of them)?

Thank you again.

Jim

Q: When using it with O365, does the Sent email show up in the Sender’s Outlook account?

A: My example is using SMTP to send the email. In my experience, SMTP email does who up in the sent email box.

Q: Also, when I set up the Oauth account under the company, can it then be used by the individual employees (vs. having to set up an account for each of them)?

A: The Oauth is simply a means to replacing password access. Your organization is registered with microsoft/google/etc to ask for the User to login and give access to their account. Therefore any user with any email can use this method to start using email. If you plan to have multiple emails then you will have to store the proper login and token information.

Jeff Hojka

Thank you Jeff.

We’ve been using Postmark for sending via smtp, so we could move on from that. What about sending via IMAP – which is probably more in line with what we want to accomplish.

IMAP is a receiving protocol not a sending protocol.

Thanks for the clarification.

Is there a way to send an email from our application and have it in the user’s (O365) Sent mail? The email contains detailed specs that are generated in the app. The salesperson in this case needs to have the email thread in Outlook. Currently, they cc it to themselves but it’s not ideal.

I would assume IMAP is also supported but I have never gotten that to work properly with NetTalk. So I didn’t pursue that option. I think Andy’s tools also has IMAP functionality. I dont know if clarion ever built that into their toolbox. Regardless of the tool you use. The Oauth part of this is again, simply a replacement for the typical login and password. If you can login to email using IMAP that way you should also be able to do the same using Oauth. You should also be able to switch tools. Use Netatlk for Oauth and Andy’s tools for IMAP. I do not know much about Andy’s IMAP stuff. But his uses chillkat which has much more support for these type of communications. Things such as support for more servers. I had the same experience with FTP and IS servers.

Jeff

Q: Is there a way to send an email from our application and have it in the user’s (O365) Sent mail?

A: My answer is going to be a split answer.

Using SMTP currently I send emails for a variety of reasons using our O365 account. These all show up in my Sent Box.

I have yet to incorporate OAuth into those applications. Because of the nature of how SMTP works I will say there is a very good chance the everything works exactly the same. Once you have your Oauth Tokens

Jeff

Thank you Jeff! Will start with smtp and look into IMAP if that fails to work for us.

Appreciate the help!

I created an example app that uses OAuth to Connect to GMail. It requires C11.1, Nettalk 14, String Theory, Refllection and JFiles. There’s an app, cwproj and sln. I also included the generated source.
GMailOAuth.zip (220.3 KB)

4 Likes

Hate to say it but I’m still having issues trying to send an email via Office365. I get the token back just fine but it times-out when trying to connect to smtp.office365.com.

Unfortunately, I’m still on Clarion 10 so can’t open Jim’s example for gmail. I did look at the source code and appreciate receiving that. My guess for now is that I’ve got something wacked on the MS side.

If anyone has a working app for Clarion 10 that I could plug my credentials into and is willing to share it, please do. That will hopefully rule out problems in my code.

Thank you!

Here is a TXA the program has no dictionary. You may have to turn on that option in SV somewhere or it may not import or whatever the IDE might complain about. See if you can import this. I used the resources from the Nettalk oauth example app. You can get the images and icons from there. Hopefully you get it going.

The file here

NetOauth2Email.zip (92.5 KB)

Thank you Jeff! I really appreciate it!

1 Like

Its is not working for me, I always get an error in the browser


Any idea?
Thanks

Theres not much to go on, but i suspect your client id is wrong. Perhaps you mixed it up with the client secret?

Please re-read the section on setting up your account. It is very easy to take the wrong “id”. I was confused at first and had to make sure I used the correct information. Also double check the section on permissions.

Ok, its working now, question: what happen when the token must be renew? the user must log in again?

Ok, its working now, question: what happen when the token must be renew? the user must log in again?