Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
WebDAV .NET for Exchange
>
Tutorial
> Download attachments
The following example shows you how to get and download attachments from a message.
C# example
using System; using System.IO; using System.Net; using Independentsoft.Webdav.Exchange; namespace Sample { class Program { static void Main(string[] args) { NetworkCredential credential = new NetworkCredential("username", "password"); WebdavSession session = new WebdavSession(credential); Resource resource = new Resource(session); string messageUrl = "https://myserver/exchange/emailaddress/Inbox/message1.eml"; Attachment[] attachment = resource.GetAttachment(messageUrl); for (int k = 0; k < attachment.Length; k++) { string atachmentUrl = attachment[k].Address; string atachmentFileName = attachment[k].FileName; if (atachmentFileName == null) { atachmentFileName = atachmentUrl.Substring(atachmentUrl.LastIndexOf("/") + 1); } FileStream file = new FileStream("c:\\temp\\" + atachmentFileName, FileMode.CreateNew); Stream stream = resource.GetInputStream(attachment[k].Address); byte[] buffer = new byte[2048]; int len = 0; while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, len); } file.Close(); stream.Close(); } } } }
VB example
Imports System Imports System.IO Imports System.Net Imports Independentsoft.Webdav.Exchange Module Module1 Sub Main(ByVal args() As String) Dim credential As NetworkCredential = New NetworkCredential("username", "password") Dim session As WebdavSession = New WebdavSession(credential) Dim resource As Resource = New Resource(session) Dim messageUrl As String = "https://myserver/exchange/emailaddress/Inbox/message1.eml" Dim attachment() As Attachment = resource.GetAttachment(messageUrl) Dim k As Integer For k = 0 To attachment.Length - 1 Dim atachmentUrl As String = attachment(k).Address Dim atachmentFileName As String = attachment(k).FileName If atachmentFileName Is Nothing Then atachmentFileName = atachmentUrl.Substring(atachmentUrl.LastIndexOf("/") + 1) End If Dim file As FileStream = New FileStream("c:\\temp\\" + atachmentFileName, FileMode.CreateNew) Dim stream As Stream = resource.GetInputStream(attachment(k).Address) Dim buffer() As Byte = New Byte(2048) {} Dim len As Integer = stream.Read(buffer, 0, buffer.Length) While (len > 0) file.Write(buffer, 0, len) len = stream.Read(buffer, 0, buffer.Length) End While file.Close() stream.Close() Next End Sub End Module