Automate  text capture from ms exchange email account

LuckyLucks
LuckyLucks used Ask the Experts™
on
Hi

Best way to download email body from MS exchange to text file, in an automated fashion?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can use the EWS script for the same, you can check the one below.

.\Download-Email-EWS.ps1 -ExportMsgFormatTypeTXT -userName admin@domain.com -password Password -mailbox user1@domain.com -dir "d:\EmailDownloadLocation"

https://github.com/sunilkms/O365-Powershell/tree/master/Export-Email-EWS

Author

Commented:
Could you attach / paste the script? thx
param (
[Switch]$report=$False,
[Switch]$exportEML=$false,
[Switch]$ExportMsgFormatTypeTXT=$false,
$mailbox="Sunil.chauhan@Domain.com" ,
$itemsView=1000 ,                                       			                  #No of Items to Export from the Folder
$dir="D:\WindowsPowerShell\Download-Email\" ,                                   #Folder Path to save items in
$userName = "userid@Domain.com" ,
$password ="password"
)

$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

#Setup EWS Service Client
$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

#Entering into Mailbox
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$Mailbox);  

$MailboxRootid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$mailbox)
$MailboxRoot=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$MailboxRootid)

#Loading All Folders Under Recoverable Items Root
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(50)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
$findFolderResults | ? {$_.FolderClass -match "IPF.Note"} |ft DisplayName, TotalCount, FolderClass
Write-Host "Type the folder name to export" -f yellow
$folder=read-host "Folder to Export:"
$folderID = ($findFolderResults | ? {$_.DisplayName -eq $folder}).ID

if ($ExportMsgFormatTypeTXT) {
#Setup property set for email
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet `
([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::text

#Loading Email Items
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList $ItemsView
$view.PropertySet = $propertyset
$items=$service.FindItems($folderid,$View)
$items.load($psPropset)

$items.items | select Subject,LastModifiedTime,DateTimeReceived,Sender,{$_.ToRecipients}
	foreach ($item in $items.items)      
			{
			Write-Host "Writing Email to File:" $item.Subject
			$fileName = $Dir + $($item.Subject).replace(":","-") + ".txt"
			$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
			$item.load($psPropset)
			$Email = new-object System.IO.FileStream($fileName, [System.IO.FileMode]::Create)
			$Email.Write($Item.MimeContent.Content, 0,$Item.MimeContent.Content.Length)
			$Email.Close()
			}
} else {
#Setup property set for email
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet `
([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML

#Loading Email Items
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList $ItemsView
$view.PropertySet = $propertyset
$items=$service.FindItems($folderid,$View)
$items.load($psPropset)
     }
	 
if ($report) {
		$items.items | ft Subject,LastModifiedTime,DateTimeReceived,Sender,{$_.ToRecipients}
             }
	   
if ($exportEML) {
        foreach ($item in $items.items)
        {
        Write-Host "Writing Email to File:" $item.Subject
        $fileName = $Dir + $($item.Subject).replace(":","-") + ".eml"
        $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
        $item.load($psPropset)
        $Email = new-object System.IO.FileStream($fileName, [System.IO.FileMode]::Create)
        $Email.Write($Item.MimeContent.Content, 0,$Item.MimeContent.Content.Length)
        $Email.Close()
        }
 }

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
This needs to be run on the Exchange server?
The Script uses the EWS API,  you can run it from the normal PowerShell on a system where you have eWS api installed, make sure to correct the  EWS URL pointing to your CAS if its an on-premise setup.

Author

Commented:
needed to download t access and review

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial