Using OAuth for authentication to connect with SMTP, IMAP, and POP3

Requirements: IPWorks

Our SMTP based components (SMTP, FileMailer, HTMLMailer) as well as our IMAP and POP components can use OAuth as a form of Authentication.

Contents

OAuth Authentication

Some environments may require authentication using OAuth when connecting via SMTP, IMAP, or POP. For instance, this is required when conneting to Gmail or Office365. The OAuth component can be used to obtain an authorization string for use with OAuth in SMTP, IMAP, or POP. The current version of the OAuth component implements OAuth 2.0.

GMail

Authenticating with IMAP:

//Getting an authorization string
oauth.ClientId = "723966830965.apps.googleusercontent.com";
oauth.ClientSecret = "_bYMDLuvYkJeT_99Q-vkP1rh";
oauth.ServerAuthURL = "https://accounts.google.com/o/oauth2/auth";
oauth.ServerTokenURL = "https://accounts.google.com/o/oauth2/token";
oauth.AuthorizationScope = "https://mail.google.com/";
string authorization = oauth.GetAuthorization();

//Setting Gmail server settings
imap.MailServer = "imap.gmail.com";
imap.User = "test@email.com";
imap.SSLStartMode = IPWorks.ImapSSLStartModes.sslImplicit;
imap.MailPort = 993;
 
//Authenticating using XOAuth2
imap.AuthMechanism = IPWorks.ImapAuthMechanisms.amXOAUTH2;
imap.Config("AuthorizationIdentity=" + authorization);
imap.Connect();
//Additional code here
imap.Disconnect();

Authenticating with SMTP:

//Getting an authorization string
oauth.ClientId = "723966830965.apps.googleusercontent.com";
oauth.ClientSecret = "_bYMDLuvYkJeT_99Q-vkP1rh";
oauth.ServerAuthURL = "https://accounts.google.com/o/oauth2/auth";
oauth.ServerTokenURL = "https://accounts.google.com/o/oauth2/token";
oauth.AuthorizationScope = "https://mail.google.com/";
string authorization = oauth.GetAuthorization();
 
//Setting Gmail server settings
smtp.From = "test@email.com";
smtp.SendTo = "test@email.com";
smtp.Subject = "Test";
smtp.Message = "Hello, this is a test";
smtp.MailServer = "smtp.gmail.com";
 
//Authenticating using XOAuth2
smtp.AuthMechanism = IPWorks.HtmlmailerAuthMechanisms.amXOAUTH2;
smtp.Config("AuthorizationIdentity=" + authorization);
smtp.Message = "Test Mail";
smtp.Connect();
smtp.Send();
smtp.Disconnect();

Authenticating with POP:

//Getting an authorization string
oauth.ClientId = "723966830965.apps.googleusercontent.com";
oauth.ClientSecret = "_bYMDLuvYkJeT_99Q-vkP1rh";
oauth.ServerAuthURL = "https://accounts.google.com/o/oauth2/auth";
oauth.ServerTokenURL = "https://accounts.google.com/o/oauth2/token";
oauth.AuthorizationScope = "https://mail.google.com/";
string authorization = oauth.GetAuthorization();

//Setting Gmail server settings
pop.MailServer = "pop.gmail.com";
pop.User = "test@email.com";
pop.SSLStartMode = IPWorks.ImapSSLStartModes.sslImplicit;
pop.MailPort = 993;
 
//Authenticating using XOAuth2
pop.AuthMechanism = IPWorks.ImapAuthMechanisms.amXOAUTH2;
pop.Config("AuthorizationIdentity=" + authorization);
pop.Connect();
//Additional code here
pop.Disconnect();

Office365

Authenticating with IMAP:

//Getting an authorization string
oauth.ClientId = "3c65828c-6376-4286-91b5-2381c3904a97";
oauth.ClientSecret = "mkk2a2M-B5TQI7o5p_N0fR-CHYVn7e3yH~";
oauth.ServerAuthURL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
oauth.ServerTokenURL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
oauth.AuthorizationScope = "https://outlook.office.com/IMAP.AccessAsUser.All";
string authorization = oauth.GetAuthorization();

//Setting Office365 server settings
imap.MailServer = "outlook.office365.com";
imap.User = "test@email.com";
imap.SSLStartMode = IPWorks.ImapSSLStartModes.sslImplicit;
imap.MailPort = 993;
 
//Authenticating using XOAuth2
imap.AuthMechanism = IPWorks.ImapAuthMechanisms.amXOAUTH2;
imap.Config("AuthorizationIdentity=" + authorization);
imap.Connect();
//Additional code here
imap.Disconnect();

Authenticating with SMTP:

oauth.ClientId = "3c65828c-6376-4286-91b5-2381c3904a97";
oauth.ClientSecret = "mkk2a2M-B5TQI7o5p_N0fR-CHYVn7e3yH~";
oauth.ServerAuthURL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
oauth.ServerTokenURL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
oauth.AuthorizationScope = "https://outlook.office.com/SMTP.Send";
oauth.GrantType = OauthGrantTypes.ogtAuthorizationCode;

//Setting Office365 server settings
smtp.From = "test@email.com";
smtp.SendTo = "test@email.com";
smtp.Subject = "Test";
smtp.Message = "Hello, this is a test";
smtp.MailServer = "smtp.office365.com";
smtp.MailPort = 587;

smtp.AuthMechanism = SmtpAuthMechanisms.amXOAUTH2;
smtp.Config("AuthorizationIdentity=" + oauth.GetAuthorization());           
smtp.SSLStartMode = SmtpSSLStartModes.sslExplicit;

smtp.Connect();
smtp.Send();
smtp.Disconnect();

Authenticating with POP:

//Getting an authorization string
oauth.ClientId = "3c65828c-6376-4286-91b5-2381c3904a97";
oauth.ClientSecret = "mkk2a2M-B5TQI7o5p_N0fR-CHYVn7e3yH~";
oauth.ServerAuthURL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
oauth.ServerTokenURL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
oauth.AuthorizationScope = "https://outlook.office.com/POP.AccessAsUser.All";
string authorization = oauth.GetAuthorization();

//Setting Office365 server settings
pop.MailServer = "outlook.office365.com";
pop.User = "test@email.com";
pop.SSLStartMode = IPWorks.PopSSLStartModes.sslImplicit;
pop.MailPort = 995;
 
//Authenticating using XOAuth2
pop.AuthMechanism = IPWorks.PopAuthMechanisms.amXOAUTH2;
pop.Config("AuthorizationIdentity=" + authorization);
pop.Connect();
//Additional code here
pop.Disconnect();

We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.