Saving Attachments thru WebDav

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael OC

    #1

    Saving Attachments thru WebDav

    Hi,

    I have a somewhat working solution that will go into the mailbox and get the attachments and save them to a folder.

    My issue is that sometimes it works fine, while others it gives me a 404 error.

    As I debug the solution I can see that the app. can see the emails and the attachments, but then will 404 error out. Here is code for DownloadSingleA ttachment().

    Code:
    HttpWebRequest WebReq;
    WebReq = (HttpWebRequest)WebRequest.Create(emlURL + "/" + filename);
    
            try
            {
                if (this.myCred.UserName.Length != 0)
                {
                    // Use Basic Authentication
                    CredentialCache myCredentialCache = new  CredentialCache();
                    myCredentialCache.Add(new System.Uri(emlURL), "NTLM", myCred);
                    WebReq.Credentials = myCredentialCache;
                }
                else
                {
                    // Use Windows Authentication
                    WebReq.Credentials = CredentialCache.DefaultCredentials;
                }
    
                //Add the headers
                WebReq.Headers.Add("Translate", "f");
                WebReq.KeepAlive = true;
                WebReq.AllowAutoRedirect = false;
                WebReq.Method = "GET";
    
                // Get the response for our request
                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    
    switch (WebResp.ContentType)
    {
       case "application/octet-stream":
                        Stream tmpStream = WebResp.GetResponseStream();
    
                        FileStream file = new FileStream(path + "\\" + newFilename, System.IO.FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, false);
    
                        byte[] buffer = new byte[4096];
                        int length;
                        length = tmpStream.Read(buffer, 0, 4096);
    
                        while (length > 0)
                        {
                            file.Write(buffer, 0, length);
                            length = tmpStream.Read(buffer, 0, 4096);
                        }
    
                        file.Close();
                        tmpStream.Close();
                        break;
    }
Working...