Downloading attachments from Exchange with WebDAV

Posted by arturito on Stack Overflow See other posts from Stack Overflow or by arturito
Published on 2010-03-22T10:58:23Z Indexed on 2010/03/22 11:01 UTC
Read the original article Hit count: 665

Filed under:
|
|

Hi there! I've been trying to retrive attachment from message in Exchange server using WebDAV.I don't know which version of Exchange the company is running.

I'can successfully read messages and retrive list of attachments. However I am failing to save attachments. In both cases errors is:

"The remote server returned an error: <403> Forbidden.

Any idea what I'm doing wrong? My code:

        static void Main(string[] args)
    {

        HttpWebRequest Request;
        WebResponse Response;
        CredentialCache MyCredentialCache;
        string attachment = "http://mailserver/Exchange/Username/Inbox/Test.EML/Test.txt";
        string strUserName = "username";
        string strPassword = "password";
        string strDomain = "domain";

        try
        {
            // HttpWebRequest
            MyCredentialCache = new System.Net.CredentialCache();
            MyCredentialCache.Add(new System.Uri(attachment), "NTLM", new NetworkCredential(strUserName, strPassword, strDomain));

            Request = (HttpWebRequest)HttpWebRequest.Create(attachment);
            Request.Credentials = MyCredentialCache;
            Request.Method = "GET";
            Response = (HttpWebResponse)Request.GetResponse();
        }
        catch(Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }

        try
        {
            //Web Client 
            string downloadPath = "D:\\Downloads";

            WebClient wcClient = new WebClient();
            wcClient.Credentials = new NetworkCredential(strUserName, strPassword, strDomain);
            string file = Path.GetFileName(attachment);
            string filename = Path.Combine(downloadPath, file);
            wcClient.DownloadFile(attachment, filename);
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }

        Console.ReadLine();

    }

© Stack Overflow or respective owner

Related posts about webdav

Related posts about exchange