Skip to content

Instantly share code, notes, and snippets.

@gscales
Created April 1, 2022 04:55
Show Gist options
  • Save gscales/0a1a9964af33132fbc2de1c1055b9a56 to your computer and use it in GitHub Desktop.
Save gscales/0a1a9964af33132fbc2de1c1055b9a56 to your computer and use it in GitHub Desktop.
Example for using OAuth against Office365 in Open Pop using fork https://github.com/gscales/hpop , MSAL and ROPC
NetworkCredential networkCredential = new NetworkCredential("[email protected]", "blah");
PublicClientApplicationBuilder pcaConfig = PublicClientApplicationBuilder.Create("d64799fe-dfb2-480b-a3be-a7a5a0bdaf32").WithTenantId("eb8db77e-65e0-4fc3-b967-b14d5057375b");
var app = pcaConfig.Build();
var tokenResult = app.AcquireTokenByUsernamePassword(new string[] { "https://outlook.office.com/POP.AccessAsUser.All" }, networkCredential.UserName, networkCredential.SecurePassword).ExecuteAsync().GetAwaiter().GetResult();
var saslformatedToken = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("user=" + networkCredential.UserName + (char)1 + "auth=Bearer " + tokenResult.AccessToken + (char)1 + (char)1));
var client = new Pop3Client();
client.Connect("outlook.office365.com", 995, true);
client.Authenticate(networkCredential.UserName, saslformatedToken, AuthenticationMethod.XOAUTH2);
int messageCount = client.GetMessageCount();
var lastMessage = client.GetMessage(messageCount);
Console.Write("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment