Install ADFS on Azure VM step by step

[Powershell Script] Import eml file to user mailbox (specific folder) using EWS managed API

Import email we exported as ".eml" using the script we discussed in my last post below into user mailbox using PowerShell script.

Exporting Emails from Office 365 and Exchange Mailbox Purges folder using EWS managed API


you can add the credentials to PowerShell sessions in "$cred" variable this script will get your admin credentials automatically, you can provide them at the time of running using -UserName and -Password parameters.

Usage Example :Password from the Console sessions
$cred = Get-Credential
Import-Eml.ps1 -$mailbox "Test@domain.com" -importFromFolder "C:\mails" -SaveInFolder "ImportedEmails"

Usage Example:  Specific Userid and password for current Run only.
Import-Eml.ps1 -$mailbox "Test@domain.com" -importFromFolder "C:\mails" -SaveInFolder "ImportedEmails" -UserName "adminid@domain.com" -Password "AdminPass"

This script is going to use the EWS managed API, please met the below requirements.

  • Admin account should have application impersonation rights, you can follow this msdn post to setup the permissions.
  • Install EWS managed API on system where you plan to run the script from, if API is not installed, download the same from here

param (
$mailbox="sunil.chauhan@xyz.com",
$userName=$cred.UserName,
$password=$cred.GetNetworkCredential().password,
$importFromFolder,
$SaveinFolder
)

$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$service.url = $uri
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$Mailbox);

#Get folder to import
$MailboxRootid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$Mailbox)
$MailboxRoot=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$MailboxRootid)
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
$allFolders=$findFolderResults | ? {$_.FolderClass -eq "IPF.Note" } | select ID,Displayname
$DI = $allFolders | ? {$_.DisplayName -eq $SaveinFolder}
$Folderid=$DI.ID

#Crete Email Object
$emailsinfolder=Get-ChildItem $ImportFromFolder | ? {$_.name -like "*.Eml" }

write-host "Total Item Fount to be imported:" $emailsinfolder.count
foreach ($email in $emailsinfolder) {
$EmailtoImport= $importFromFolder + "\" + $email.Name
Write-host "Importing Email:" $EmailtoImport
$UploadEmail = new-object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
#Read File
[byte[]]$EmailinByte=get-content -encoding byte $EmailtoImport
#Set Mime Content in Message
$UploadEmail.MimeContent = new-object Microsoft.Exchange.WebServices.Data.MimeContent("us-ascii", $Emailinbyte);
$PR_Flags = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition `
(3591, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
$UploadEmail.SetExtendedProperty($PR_Flags,"1")
$UploadEmail.Save($Folderid)
}

Comments