Jump to content

Recommended Posts

Posted

Does anyone have a way to automatically download attachments for a 365 email account and save them to a folder. I'm sure I've seen it mentioned on here before, but cant find it now.

 

I want staff to be able to email pictures to an email address and they are downloaded and saved to a folder.

 

Should be fairly easy to script, but if someone has already done it will save me some time :-)

 

Thanks

 

Steve

Posted

Could try this Powershell script (EWS managed API) - third result via Google.

 

There's no content type for images afaik but you could easily use Where-Object with Remove-Item to delete anything that isn't a certain filetype after it downloads the attachments.

 

# Destination folder
$destinationFolder = "C:\Users\username\Downloads\Attachment Downloader"

# replace with your email address
$email    = "[email protected]"
$username = "[email protected]"
$password = "Password123"

# load the assembly
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"

# Create Exchange Service object
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
$s.Credentials = New-Object Net.NetworkCredential($username, $password)
# $s.TraceEnabled = $true
Write-Host "Trying AutoDiscover... "
$s.AutodiscoverUrl($email, {$true})

if(!$s.Url) {
   Write-Error "AutoDiscover failed"
   return;
} else {
   Write-Host -ForegroundColor Green "AutoDiscover succeeded - $($s.Url)"
}

# Create destination folder
$destinationFolder = "{0}\{1}" -f $destinationFolder, (Get-Date -Format "yyyyMMdd HHmmss")
mkdir $destinationFolder | Out-Null

# get a handle to the inbox
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

#create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

# Find the items
Write-Host "Searching for items in mailbox... " -NoNewline
$items = $inbox.FindItems(500)
Write-Host -ForegroundColor Green "found $($items.items.Count)"

$inc = 0;
foreach ($item in $items.Items)
{
   # Create mail folder
   $inc += 1
   $mailFolder = "{0}\{1}" -f $destinationFolder, $inc; 
   mkdir $mailFolder | Out-Null

   # load the property set to allow us to get to the body
   $item.load($psPropertySet)
   Write-Host ("$inc - $($item.Subject)") -ForegroundColor Yellow

   # save the metadata to a file
   $item | Export-Clixml ("{0}\metadata.xml" -f $mailFolder)

   # save all attachments
   foreach($attachment in $item.Attachments) {
       if($attachment.ContentType -notlike "message/*") {
           Write-Host " - $($attachment.Name) ($($attachment.ContentType))"
           $fileName = "{0}\{1}" -f $mailFolder, $attachment.Name
           $attachment.Load($fileName)
       }
   }

   # delete the mail item
   $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete, $true)
}

  • Thanks 1
Posted

I will restrict the email account to only receive emails from internal staff accounts.

 

The plan is for it to be used as part of a display in our nursery. All the staff have an ipad, and frequently take pictures of the children or work, print them off and display them, changing them every few days. It's the speed of having their work up on the wall for children that age that is key for them and has a big impact. The idea therefore is they will email the pictures to a display email account, it will download them, and have a slideshow of the last 20 pictures it receives. That side of things is fairly strait forward, it was just getting it to download them that was the issue.

 

Thanks Arreks, I will give that one a go, the ones that I found when I googled were all macros for outlook, which wasn't ideal.

 

Steve

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...