This article describes how to use
the
HttpWebRequest class and the
HttpWebResponse class from the
System.Net namespace to copy and move
items on a computer that is running Microsoft Exchange 2000 Server by using Microsoft Visual C#.
This article assumes that you are
familiar with the following topics:
- Programming with Visual C#
- The HttpWebRequest class and the HttpWebResponse class
- The Web Distributed Authoring and Versioning (WebDAV) protocol
The following list outlines the
recommended hardware, software, network infrastructure, and service packs that
you need:
- Microsoft Visual Studio .NET or Microsoft Visual 2005
- Exchange 2000
To copy and move items on a computer that is running Exchange 2000 by using Visual C# , follow these steps:
- Start Visual Studio .NET or Visual Studio 2005.
- On the File menu, point to New, and then click Project.
- In the Visual C# Projects typeslist, click Console Application.
Note In Visual Studio 2005, click Console Application in the Visual C# list.
In Visual Studio .NET, the Class1.cs file is created by default. In Visual Studio 2005, Program.cs is created by default. - In the code window, replace the code with the following:
using System;
using System.Net;
using System.IO;
namespace WebDavNET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
try
{
bool bMove;
// TODO: Uncomment the following line to Move
// bMove = true;
// TODO: Uncomment the following line to Copy
// bMove = false;
// TODO: Replace with the source URL of an item
String sSourceURL = "http://ExchServer/Public/Folder1/test1.eml";
// TODO: Replace with the destination URL of an item
String sDestinationURL = "http://ExchServer/Public/Folder2/test2.eml";
System.Uri myUri;
myUri = new System.Uri(sSourceURL);
HttpWebRequest HttpWRequest;
HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
// Set credentials.
NetworkCredential myCred;
// TODO: Replace with the appropriate username and password
myCred = new NetworkCredential(@"DomainName\UserName", "UserPassword");
CredentialCache myCredentialCache;
myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myCred);
HttpWRequest.Credentials = myCredentialCache;
// Set headers.
HttpWRequest.KeepAlive = false;
HttpWRequest.Headers.Set("Pragma", "no-cache");
HttpWRequest.Headers.Set("Destination", sDestinationURL);
//Set the request timeout to 5 minutes.
HttpWRequest.Timeout = 300000;
// set the request method
if(bMove)
{
HttpWRequest.Method = "MOVE";
}
else
{
HttpWRequest.Method = "COPY";
}
// Send the request and get the response.
HttpWebResponse HttpWResponse;
HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
// Get the Status code.
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
Console.WriteLine("Status Code: {0}", sStatus);
// Get the request headers
string sReqHeaders = HttpWRequest.Headers.ToString();
Console.WriteLine(sReqHeaders);
// Read the response stream.
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
Console.WriteLine("Response: {0}", sText);
// Close the stream.
strm.Close();
// Clean up
myCred = null;
myCredentialCache = null;
HttpWRequest = null;
HttpWResponse = null;
strm = null;
sr = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
} - Search for TODO in the code, and then modify the code for your environment.
- Press F5 to build and to run the program.
- Make sure that the item was copied and moved.
For more information, visit the following MSDN Web sites: