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: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
// Learn more about F# at http://fsharp.net

open Microsoft
open Microsoft.Exchange.WebServices.Data
open System
open System.Net

ServicePointManager.ServerCertificateValidationCallback <- ( fun _ _ _ _ -> true )

let exchangeService =    
    let service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)    
    service.Url <- new Uri("https://ip_exchange/EWS/Exchange.asmx")
    service.Credentials <- new WebCredentials("user", "password", "domain")            
    service

let readMailBox =         
    let items = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(10))    
    let itemsEnumerator = items.GetEnumerator()    
    seq { while itemsEnumerator.MoveNext() do yield itemsEnumerator.Current }

let processItem messageID =
    let emailMessage = EmailMessage.Bind( exchangeService, messageID, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments)) 
    let attachmentEnumerator = emailMessage.Attachments.GetEnumerator()
    let attachments = seq { while attachmentEnumerator.MoveNext() do
                                 yield attachmentEnumerator.Current }
    let fileAttachments = attachments |> Seq.filter ( fun attachment -> match attachment with
                                                                        | :? FileAttachment -> true
                                                                        |_ -> false )
                                      |> Seq.cast
    printfn "saving"
    for fileAttachment : FileAttachment in fileAttachments do         
        let fileName = String.concat "" ["c:\\temp\\"; fileAttachment.Name ] 
        fileAttachment.Load(fileName)
                
let main = 
    // read all items from mailbox and ...    
    let items = readMailBox       
    // filter so we only have items with attachements and ...
    let itemsWithAttachments = items |> Seq.filter ( fun item -> item.HasAttachments ) 
    // process every item based in ID
    for item in itemsWithAttachments do processItem item.Id                      

main
namespace Microsoft
namespace Microsoft.FSharp.Data
namespace System
namespace System.Net
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
val exchangeService : obj

Full name: Script.exchangeService
val service : obj
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 readMailBox : seq<obj>

Full name: Script.readMailBox
val items : obj
val itemsEnumerator : obj
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 processItem : messageID:'a -> unit

Full name: Script.processItem
val messageID : 'a
val emailMessage : obj
val attachmentEnumerator : obj
val attachments : seq<obj>
val fileAttachments : seq<obj>
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val attachment : obj
val cast : source:Collections.IEnumerable -> seq<'T>

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

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val fileAttachment : obj
val fileName : string
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val main : unit

Full name: Script.main
val items : seq<obj>
val itemsWithAttachments : seq<obj>
val item : obj
Next Version Raw view Test code New version

More information

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