Access emails in Shared mailbox

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dermot Carey Dermot Carey 2 years, 8 months ago.

  • Author
    Posts
  • #6529

    by bdizzle at 2013-03-07 07:21:40

    I'm trying to access emails in a shared mailbox I have access to and download attachments stored in the mailbox. I got the script successfully working on my personal inbox using the getdefaultfolder method, but I'm not quite show how to do it with the shared folder. Here's the code I have so far.


    $filepath = "c:\tmp"
    $outlook = New-Object -comobject outlook.application
    $namespace = $outlook.application.GetNamespace("MAPI")
    $folder = $namespace.GetDefaultFolder(6)

    $outlookItems = $folder.Items | where {$_.sendername -eq "last.first"} | where {$_.attachments -ne $null}
    foreach ($item in $outlookItems) {
    $colAttachments = $item.attachments
    foreach ($attachment in $colAttachments) {
    If ($attachment.fileName.Contains("csv")) {
    $attachment.saveasfile((Join-Path $filepath "$($attachment.filename)"))
    }
    }
    }

    How do I tell the script to look in the inbox folder of the shared mailbox?

    Thanks

    by DonJ at 2013-03-07 07:59:51

    I hate to do this – but you might want to post this elsewhere. It isn't really a PowerShell question; you need some help using the Outlook COM object. StackOverflow might be a good start. Someone may pop in here with a tip, so check back, but I don't want you waiting around getting nothing.

    by ArtB0514 at 2013-03-07 09:11:43

    Some more information....

    The folder you are looking for isn't one of the default folders, so you'll need to find it's name before you can manipulate its content. To get the list of top level folders, do this:
    $namespace.Folders | foreach {$_.Name}
    When you know which toplevel folder you want to use (example: "Outlook"), this will get you the names of the next level folders:
    $namespace.Folders.Item("Outlook").Folders | foreach {$_.Name}
    That should get you the name of the folder you want (example: "Shared"). Then set $Folder to this and you should be ready to go.
    $Folder = $namespace.Folders.Item("Outlook").Folders.Item("Shared")

    by bdizzle at 2013-03-13 06:02:36

    I figured it out so I'll share the code in case anyone else has the same problem. I was able to use Exchange Web Services to get it done. Using the com object requires an Outlook profile to be created. EWS allows you to contact the Exchange Server without need Outlook installed or configured on the computer.

    Import-Module -name "c:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll" #Imports the modules needed for Exchange Web Services
    $filepath = "\\server\share\" #Loction where the downloaded attachments will be stored
    $exchVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1 #Specifies the version of Exchange in use in the environment
    $exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchVersion) #connects to the Exchange service
    $cred = New-Object System.Net.NetworkCredential("user", "password", "domain")
    $exchService.Url = "https://webmail.domain.com/ews/exchange.asmx" #the url used to find the connection information for the shared mailbox
    $exchService.Credentials = $cred #sets the credentials used by EWS
    $mb = New-Object Microsoft.Exchange.WebServices.Data.Mailbox("shared_mailbox@domain.com") #specifies which mailbox EWS should connect to
    $folder = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $mb) #specifies which folder in the the mailbox EWS should connect to
    $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView("10") #speficies how many objects to return when the search is ran
    $searchFilters = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ContactSchema]::InternetMessageHeaders, "johnathan") #searches for all emails sent from Johnathan

    $list = $exchService.FindItems($folder, $searchFilters, $view) #searches the mailbox, using the specified filter and view
    foreach ($msg in $list.Items) { #
    $msg.Load() #the load method downloads the email message so the attachments can be retrieved
    foreach ($attachment in $msg) {
    $attachment.Attachments[0].load() #$attachment is a collection of all of the attachments in $msg. If the load method is not called, the attachments will not be able to be downloaded
    $newName = $attachment.Attachments[0].name.replace(".csv", " ($($msg.DateTimeReceived.ToShortDateString().Replace("/", "-"))).csv") #Specifies what name the attachment should be saved as. It takes the attachments original name and adds the date the message was received to the file name
    Set-Content -Value $attachment.Attachments[0].content -Encoding:Byte -Path "$($filepath)$($newName)" #saves the message to disk
    $msg.Delete("MoveToDeletedItems") #sends the message to the deleted items folder in Outlook
    }
    }

  • #18141
    Profile photo of Dermot Carey
    Dermot Carey
    Participant

    Thank you very much. This was really helpful. You helped me with 2 things that I have not be able to figure out and have been trolling the web for days. (hopefully this will help with search results)

    1. powershell how to search for emails from a sender
    2. pwershell how to search emails in a group mailbox

You must be logged in to reply to this topic.