1 people like it.

Save attachments from exchange inbox

Save the attachments of the first 10 mails to disk

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
open Microsoft
open Microsoft.Exchange.WebServices.Data
open System
open System.Net

type PgzExchangeService(url,user,password) =
    let service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
    do        
       ServicePointManager.ServerCertificateValidationCallback <- ( fun _ _ _ _ -> true )
       service.Url <- new Uri(url)
       service.Credentials <- new WebCredentials(user, password, "pgz")

    member this.Service with get() = service
    member this.InboxItems = this.Service.FindItems(WellKnownFolderName.Inbox, new ItemView(10))
    member this.GetFileAttachments ( item : Item ) =        
           let emailMessage = 
               EmailMessage.Bind( this.Service, 
                                  item.Id, 
                                  new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments))
           item, emailMessage.Attachments |> Seq.choose (fun attachment -> match box attachment with  
                                                                           | :? FileAttachment as x -> Some(x) | _ -> None)   
                  
let mailAtdomain = new PgzExchangeService("https://ip/EWS/Exchange.asmx", "user", "password")

let printsave (item : Item ,att : seq<FileAttachment>) =
    if (Seq.length att) > 0 then
        printfn "%A - saving %i attachments" item.Subject (Seq.length att)        
        att |> Seq.iter ( fun attachment -> printfn "%A" attachment.Name 
                                            attachment.Load(@"c:\temp\fsharp\" + attachment.Name ) )   

// filter so we only have items with attachements and ...
let itemsWithAttachments = mailAtdomain.InboxItems                            
                           |> Seq.map mailAtdomain.GetFileAttachments 
                           |> Seq.iter printsave
                           
namespace Microsoft
namespace Microsoft.FSharp.Data
namespace System
namespace System.Net
Multiple items
type PgzExchangeService =
  new : url:string * user:string * password:string -> PgzExchangeService
  member GetFileAttachments : item:'a -> 'a * seq<'b>
  member InboxItems : seq<obj>
  member Service : obj

Full name: Script.PgzExchangeService

--------------------
new : url:string * user:string * password:string -> PgzExchangeService
val url : string
val user : string
val password : string
val service : obj
type ServicePointManager =
  static val DefaultNonPersistentConnectionLimit : int
  static val DefaultPersistentConnectionLimit : int
  static member CertificatePolicy : ICertificatePolicy with get, set
  static member CheckCertificateRevocationList : bool with get, set
  static member DefaultConnectionLimit : int with get, set
  static member DnsRefreshTimeout : int with get, set
  static member EnableDnsRoundRobin : bool with get, set
  static member EncryptionPolicy : EncryptionPolicy
  static member Expect100Continue : bool with get, set
  static member FindServicePoint : address:Uri -> ServicePoint + 2 overloads
  ...

Full name: System.Net.ServicePointManager
property ServicePointManager.ServerCertificateValidationCallback: Security.RemoteCertificateValidationCallback
Multiple items
type Uri =
  new : uriString:string -> Uri + 5 overloads
  member AbsolutePath : string
  member AbsoluteUri : string
  member Authority : string
  member DnsSafeHost : string
  member Equals : comparand:obj -> bool
  member Fragment : string
  member GetComponents : components:UriComponents * format:UriFormat -> string
  member GetHashCode : unit -> int
  member GetLeftPart : part:UriPartial -> string
  ...

Full name: System.Uri

--------------------
Uri(uriString: string) : unit
Uri(uriString: string, uriKind: UriKind) : unit
Uri(baseUri: Uri, relativeUri: string) : unit
Uri(baseUri: Uri, relativeUri: Uri) : unit
val this : PgzExchangeService
member PgzExchangeService.Service : obj

Full name: Script.PgzExchangeService.Service
member PgzExchangeService.InboxItems : seq<obj>

Full name: Script.PgzExchangeService.InboxItems
property PgzExchangeService.Service: obj
member PgzExchangeService.GetFileAttachments : item:'a -> 'a * seq<'b>

Full name: Script.PgzExchangeService.GetFileAttachments
val item : 'a
val emailMessage : obj
module Seq

from Microsoft.FSharp.Collections
val choose : chooser:('T -> 'U option) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.choose
val attachment : obj
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
val x : 'b
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val mailAtdomain : PgzExchangeService

Full name: Script.mailAtdomain
val printsave : item:'a * att:seq<'b> -> unit

Full name: Script.printsave
val att : seq<'b>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val attachment : 'b
val itemsWithAttachments : unit

Full name: Script.itemsWithAttachments
property PgzExchangeService.InboxItems: seq<obj>
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
member PgzExchangeService.GetFileAttachments : item:'a -> 'a * seq<'b>

More information

Link:http://fssnip.net/aF
Posted:12 years ago
Author:Joeri Belis
Tags: .net libraries , exchange web service , ews