views:

267

answers:

2

Hi there! I've been trying to retrive attachment from message in Exchange 2003 server using WebDAV.

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

    }
A: 

I have found solution to my problem. I created a post here showing examples: http://arturito.net/2010/03/26/c-sharp-saving-email-attachments-microsof-exchange-webdav/

Arturito
A: 

Hi Arturo!

Consider also use EWS Api, WebDaw not is enabled by default in Exchange 2007 servers.

Carlos Garces
I had to write the same app for Exchange 2007 as the company changed servers last week. Here is the sample code that does the same thing but with Exchange Web Services: http://arturito.net/2010/06/14/c-sharp-accesing-shared-mailbox-in-exchange-web-services-2007-service-pack-1-and-downloading-attachments/
Arturito