OAuth 2.0 authentication

Visual Basic Code | ASP Code

This sample connects to IMAP4 server using OAuth 2.0 access token (which you need to obtain from a mail service provider using their API), logs in e-mail account, and selects "Inbox" folder.

MailBee Objects ships with VBScript OAuth 2.0 samples for Gmail and Office 365. You can find it in My Documents/MailBee Objects/Samples/OAuth2_VBScript folder.

Note that the mail service provider's APIs for obtaining access tokens can be different for web and Windows applications.

[Visual Basic]:

Dim objIMAP4, objMsg

' Create IMAP4 object
Set objIMAP4 = CreateObject("MailBee.IMAP4")

' Enable logging IMAP4 session into a file
objIMAP4.EnableLogging = True
objIMAP4.LogFilePath = "C:\Temp\imap4_log.txt"

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

' Set user credentials
objIMAP4.UserName = "username" ' Usually, email address
objIMAP4.Password = "access_token" ' You get it from the mail service provider using their proprietary API

' OAuth 2.0
objIMAP.AuthMethod = 6

' Connect to the server and log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then
    ' All is fine
    MsgBox "Inbox selected successfully"
  Else
    ' Display error information
    MsgBox "Error #" & objIMAP4.ErrCode
    MsgBox "Server response: " & objIMAP4.ServerResponse
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  MsgBox "Error #" & objIMAP4.ErrCode
  MsgBox "Server response: " & objIMAP4.ServerResponse
End If

[ASP]:

lt;%
Dim objIMAP4, objMsg

' Create IMAP4 object
Set objIMAP4 = Server.CreateObject("MailBee.IMAP4")

' Enable logging IMAP4 session into a file
objIMAP4.EnableLogging = True
objIMAP4.LogFilePath = "C:\Temp\imap4_log.txt"

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

' Set user credentials
objIMAP4.UserName = "username" ' Usually, email address
objIMAP4.Password = "access_token" ' You get it from the mail service provider using their proprietary API

' OAuth 2.0
objIMAP.AuthMethod = 6

' Connect to the server and log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then
    ' All is fine
    Response.Write "Inbox selected successfully"
  Else
    ' Display error information
    Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
    Response.Write "Server response: " & objIMAP4.ServerResponse
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
  Response.Write "Server response: " & objIMAP4.ServerResponse
End If
%>
See Also:
AuthMethod Property

 


Send feedback to AfterLogic
Copyright © 2002-2022, AfterLogic Corporation. All rights reserved.