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

Depends on the service, but generally speaking, no.
Your program will still call oAuthLogin to get the token (as it always does.)
If the current token is valid, that is returned.

If it has expired then it should get another token automatically (with no user intervention).

After some period of time (depends on the service provider) they will need to manually do the OAuth login again. This could be once a year, or never - it’s completely dependent on the service provider.

Cheers
Bruce

1 Like

Fantastic! I’m happy you got it working

Hi I’m new to clarion and using clarion8 version I want to implement OAuth ,I have Microsoft credentials (client Id ,Client Sec etc) now i refer this post and tried myself but not able to retrieve token. error is OAuthLogin is undefined.

Welcome to the community. It has been a very long time since clarion8 was released but I am not sure that would make a difference. Additionally I am not sure how you are sending email. I use Nettalk and I am on a rather current version. Could you provide more details. Most often the mistake is getting the proper key and secret. The site was very confusing and I grabbed the wrong details. Did you import and compile the program provided above?

Hi JeffH Im using NetTalk 11 version for sending mail below is the code.

IF ~CLIP(MyServer)
MESSAGE(‘There has been no SMTP mail server specified!’,‘Invalid Mail Server’,ICON:HAND)
!SELECT(?MyServer)
CYCLE
END
IF ccSender = TRUE
IF CLIP(MyCC)
MyCC = CLIP(MyCC) & '; ’ & CLIP(MyFrom)
ELSE
MyCC = CLIP(MyFrom)
END
END

ThisEmailSend.Server = MyServer
ThisEmailSend.Port = EmailPort !25
ThisEmailSend.ToList = MyTo
ThisEmailSend.ccList = MyCC
ThisEmailSend.bccList = ‘’
ThisEmailSend.From = MyFrom
ThisEmailSend.Helo = MyHelo
ThisEmailSend.Subject = MySubject
IF CLIP(AttachedFileList) !‘D:\Temp\TestReadme.txt’ ! Could be ‘c:\test.txt’
ThisEmailSend.AttachmentList = AttachedFileList !‘D:\Temp\TestReadme.txt’ ! Could be ‘c:\test.txt’
END
!ThisEmailSend.SetRequiredMessageSize (0, LEN(CLIP(MyText)), LEN(CLIP(MyHTML)))
IF UseAuth = TRUE AND CLIP(UserAuthName) <> ‘’
ThisEmailSend.AuthUser = UserAuthName
ThisEmailSend.AuthPassword = UserAuthPassword ! Case Sensitive
END
!-----------------------------------------------------------------------
IF UseSSL
ThisEmailSend.SSL = 1 ! Set to 1 to use SSL
END
IF UseTLS !CLIP(UPPER(ORG:szEmailServer)) = ‘’
ThisEmailSend.SecureEmailStartTLS = 1 ! Set to 1 to use StartTLS
END
IF ThisEmailSend.SSL OR ThisEmailSend.SecureEmailStartTLS
ThisEmailSend.SSLCertificateOptions.CertificateFile = ‘’ !CertificateFile
ThisEmailSend.SSLCertificateOptions.PrivateKeyFile = ‘’ !PrivateKeyFile
ThisEmailSend.SSLCertificateOptions.DontVerifyRemoteCertificateWithCARoot = TRUE
ThisEmailSend.SSLCertificateOptions.DontVerifyRemoteCertificateCommonName = TRUE
ThisEmailSend.SSLCertificateOptions.CARootFile = ‘’ !CARootFile
END
!End of use SSL/TLS
!-----------------------------------------------------------------------
!You must call this function
!before populating self.MesageText
IF ThisEmailSend.Error = 0 ! Check for error
ThisEmailSend.MessageTextST.SetValue(CLIP(MyText))
IF LEN(CLIP(MyHTML)) > 0
ThisEmailSend.MessageHTMLST.SetValue(CLIP(MyHTML))
END
SETCURSOR(CURSOR:WAIT)
DISPLAY()
ThisEmailSend.SendMail(NET:EMailMadeFromPartsMode) ! Put email in queue and start sending it

SETCURSOR()
DISPLAY()
ELSE
CYCLE
END

for setting up microsoft credential
! Get Passed Parameters…
MyServer = pServer
MyFrom = pFrom
MyTo = pTo
EmailPort = pPort
AttachedFileList = pAttachmentList
UserAuthName = pUserName
UserAuthPassword = pUserPassword
MyCC = pCCAddress
UseSSL = pUseSSL
UseTLS = pUseTLS
UseAuth = pUseAuth
IF ~EmailPort THEN EmailPort = 25.
MyText = pMyText
MySubject = pMySubject
ErrFlag = FALSE

clear(OAuthParms)
OAuthParms.pServiceName = ‘Microsoft’
OAuthParms.pOAuthVersion = 2
OAuthParms.pClientId = ‘
OAuthParms.pClientSecret = '

OAuthParms.pRedirectURL = ‘’
!OAuthParms.pExternalBrowser = netOAuth:UseLocalBrowser
!OAuthParms.pListenTLS = false
!OAuthParms.pListenPort = 8123
!OAuthParms.pListenBindIP = ‘127.0.0.1’
OAuthParms.pScope = ‘’
OAuthParms.pAuthorizeURL = ‘’
OAuthParms.pAccessTokenURL = ‘’
OAuthParms.pExpectedCertificateCommonName = ‘’

Result = OAuthLogin(OAuthParms)
IF Result = NET:OK
Token = OAuthParms.rToken
DISPLAY(Token)
END

I am confused by the order of your code. May I recommend that you break down your code into smaller sections as routines. Then call the routines in order. Something like.

GetOauthParamsStep1 ROUTINE
!Code for routine here

SendEMailRoutineStep2 ROUTINE
!Code for routine here

Then call these routines as

DO GetOauthParamsStep1
DO SendEMailRoutineStep2

I uploaded a zip file above that has a working example. It is in txa format and should import into C8. You can create a new solution from txa. The templates used are Chrome Explorer, Jfiles, NetTalk, Reflection and StringTheory. I honestly cant help with older versions here.

Oauth can be done without chrome explorer. When you import the routine you first would need to ignore the error about not having chrome explorer. Then change the code for pExternalBrowser to the proper property.

Oauth is more simple then it looks.

  1. register with the provider and get a ID and Key. Be careful of what they call it. Make sure you follow every single step in my document above. You must not only provide information about who you are but you will in many cases need top tell the provider the scope/permissions that you want access for. For example. On your phone you may get asked for location access or access to your camera. These are all permissions/scopes. You will need to provide these scopes for email. Check offline_access and Check SMTP.Send

  2. Now you use the nettalk procedure to ask the provider using your key and id to give your software access to an email. This request is returned on the given ip and port. This will include the token and refresh token.

  3. Send the email using the login email address and the refresh token

If you have trouble loading my txa then let me know. I honestly dont have a lot of time to examine all of your code.

Jeff

Hi Jeff Thank you for providing steps but my question is we have a function here
if OAuthLogin(OAuthParms) = net:Ok
! login successful
else
! login failed
end
in which we are passing OAuthParams Should i mannualy create this or this function is provided by NetTalk if yes"how to import this function(OAuthLogin) in my code so that i can use it.

Its a procedure in your app. Look in the NetTalk Oauth examples for it, and import rrom there.

From memory, theres also a button on the NetTalk Global extension to import it.

1 Like

I need to sent emails via Gmail. If I am correct this can be done with Nettalk en OAuth. Probably a lot of work with all the ClientSecretID’s and mixing them up etc.

But before I dive into this, two questions:

  1. If I sent emails via a Gmail account, will the sent email end up in the sent items of that Gmail account?
  2. How hard is it for a normal user to fill in all the needed credentials? Or don’t they nothing to know on ClientSecretID’s etc and is that a one time setting that only the developer mus do?

Hi Koen

  1. Yes - if you send via Gmail’s smtp then the email is stored in Gmail’s sent folder
  2. I’ve chosen to use Google’s Sign in with app passwords Sign in with app passwords - Gmail Help

My users don’t see the email password when sending - I’m using a separate app (not Nettalk) to send emails that runs as a service (Capesoft’s Self Service). The email password is encrypted using Cryptonite.

Cheers
Rohan

Hi Rohan,

  1. Great.
  2. Do I understand you correct that you only sign in to one Gmail account? SO it is you who created the app password?

If I understand my contractor right they want each user to use their own Gmail account. The article you pointed out that each user has to enter an app password themself. Might be a support issue :confused:

What I am not sure if they standard accounts or Google workspace accounts. That seem also to make a difference.

So instead of using an app password is letting them sign in with their username/password maybe a betters approach. Is such a thing possible and then still let my application sent email?

Hi Koen,
The standard users don’t get to choose the email address that’s used to send emails. The system admin can set up to 10 different email addresses to send from: e.g. Default, Invoices, Quotes etc etc.

Based on the type of email (e.g. invoice, quote…) the smtp module loads the different details for sending.

I guess I could use the same methodology for individuals. It seems a bit easier to manage and troubleshoot where the settings are effectively system wide.

The advantage of using the approach I have taken is that the system admin controls the storage of emails by choosing which provider is storing or managing sent emails. I’ve taken an agnostic approach in that you can choose whichever provider you like, Google, Microsoft, Namecheap, Smtp2Go, your own mail server etc etc.

I also give them the option to store sent emails in an “archive” which is just an SQLite database (this is useful if you choose a provider such as Smtp2Go). The admin settings allow them to send a blind copy of every email to a nominated email address if they require it (which is something a client did request). They also have an option to set the reply to email address within the admin settings e.g. Invoices reply to accounts, Quotes reply to sales etc etc

I think personal Gmail accounts can create app passwords as well as Google Workspace - Google does require that the user have 2fa turned on (that applies for Personal and Workspace) if you want to use app passwords.

At this stage I don’t provide any Oauth2 capabilities - not to say I won’t in the future if there is demand for it.

It really comes down to what you and your client agree works best for them.

Cheers
Rohan

1 Like

Hi Rohan,

That is good solution. If only the administrator has to the job that will work.

For now I have enough information to get back to the contractor to ask for more information. Thank you for sharing you ideas.

1 Like

I am late to this conversation. Please share the results of your investigations when you are able.