Skip to content

Instantly share code, notes, and snippets.

View gscales's full-sized avatar

Glen Scales gscales

View GitHub Profile
@gscales
gscales / gist:0a1a9964af33132fbc2de1c1055b9a56
Created April 1, 2022 04:55
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);
@gscales
gscales / gist:8a8eafc85029ce31f01f0a289469d46d
Created January 14, 2022 00:32
CustomTokenCredentials implmenation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.Threading.Tasks;
using System.Net;
using System.Security;
using Microsoft.Identity.Client;
using System.Net.Http;
Create a Draft message
POST https://graph.microsoft.com/v1.0/users('[email protected]')/mailfolders/drafts/messages HTTP/1.1
{
"Subject": "Test Message 1234",
"Body": {
"ContentType": "HTML",
"Content": "Rgds Glen"
},
@gscales
gscales / gist:02b32a66dfb0b11792b8a2d852188086
Last active August 22, 2021 23:53
Send Mail via Microsoft Graph using MIMEKit, Graph SDK and MSAL
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Identity.Client;
using Newtonsoft.Json;
using Microsoft.Graph;
using System.IO;
function Get-AccessTokenForGraphFromCertificate{
param(
[Parameter(Position = 1, Mandatory = $true)]
[String]
$TenantDomain,
[Parameter(Position = 2, Mandatory = $true)]
[String]
$ClientId,
[Parameter(Position = 3, Mandatory = $false)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2016" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
@gscales
gscales / gist:53b60b022618e55c7eb14dde741b97ae
Created April 20, 2021 06:13
Tag for external email messages received EWS examples
Extended property Definition for isExternalSender
$isExternalSender = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Guid]::Parse("41F28F13-83F4-4114-A584-EEDB5A6B0BFF"),"IsExternalSender", [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean)
Using it in FindItems
$ItemPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$ItemPropset.Add($isExternalSender)
$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$ivItemView.PropertySet = $ItemPropset
@gscales
gscales / gist:abe8e16ef4a6a05c37161551de1b1d6c
Created April 20, 2021 05:59
Tag for external email messages received Microsoft Graph examples
Include the IsExternalSender property in Results
https://graph.microsoft.com/v1.0/users('[email protected]')/MailFolders('Inbox')/messages/?
$Top=10&$expand=SingleValueExtendedProperties($filter=(Id eq 'Boolean {41F28F13-83F4-4114-A584-EEDB5A6B0BFF} Name IsExternalSender'))
Filter for only External Senders
https://graph.microsoft.com/v1.0/users('[email protected]')/MailFolders('Inbox')/messages/?
$Top=10&$filter=singleValueExtendedProperties/Any(ep:+ep/id+eq+'Boolean+{41F28F13-83F4-4114-A584-EEDB5A6B0BFF}+Name+IsExternalSender'+and+cast(ep/value,+Edm.Boolean)+eq+true)&$expand=SingleValueExtendedProperties($filter=(Id eq 'Boolean {41F28F13-83F4-4114-A584-EEDB5A6B0BFF} Name IsExternalSender'))
@gscales
gscales / gist:1ebb604bdbc06b7c3252d170c6939877
Created December 9, 2020 01:45
FindFolder JS Example TeamsMessagesData
function FindFolderRequest(){
var RequestString =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
' <soap:Header>' +
' <t:RequestServerVersion Version="Exchange2016" />' +
' </soap:Header>' +
' <soap:Body>' +
'<m:FindFolder Traversal="Shallow">' +
'<m:FolderShape>' +
@gscales
gscales / gist:84e2d5fc0bb9cdccf634be84510c2f64
Last active February 19, 2021 05:54
Get the TeamsMessagesDataFolder
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$TeamsMessagesDataFolderEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([System.Guid]::Parse("{E49D64DA-9F3B-41AC-9684-C6E01F30CDFA}"), "TeamsMessagesDataFolderEntryId", [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);
$psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.Add($TeamsMessagesDataFolderEntryId)
$RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$psPropset)
if ($RootFolder.TryGetProperty($TeamsMessagesDataFolderEntryId,[ref]$FolderIdVal))
{
$TeamsMessagesFolderId= new-object Microsoft.Exchange.WebServices.Data.FolderId((ConvertId -HexId ([System.BitConverter]::ToString($FolderIdVal).Replace("-","")) -service $service))
$TeamsMessagesDataFolder =