使用WebDAV从Exchange下载附件

时间:2010-03-22 10:58:24

标签: exchange-server attachment webdav

我一直在尝试使用WebDAV从Exchange 2003服务器中的邮件中恢复附件。

我可以成功阅读邮件并检索附件列表。但是我无法保存附件。在这两种情况下,错误都是:

“远程服务器返回错误:< 403> Forbidden。

知道我做错了什么吗? 我的代码:

        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();

    }

2 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我在这里创建了一个显示示例的帖子 http://arturito.net/2010/03/26/c-sharp-saving-email-attachments-microsof-exchange-webdav/

答案 1 :(得分:0)

还考虑使用EWS Api,默认情况下,Exchange 2007服务器中未启用WebDaw。