Downloading attachments from Exchange with WebDAV
- by arturito
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();
}