Microsoft KB Archive/313128

= How to send an e-mail message by using WebDAV in Visual C# =

Article ID: 313128

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition

-



This article was previously published under Q313128



SUMMARY
This article describes how to send an e-mail message by using Web Distributed Authoring and Versioning (WebDAV) in Microsoft Visual C#. This article also includes sample code that you can use to send the e-mail message. In the sample code, the HttpWebRequest class sends a request to a computer that is running Microsoft Exchange 2000 Server, and the HttpWebResponse class receives the response.

Note The HttpWebRequest class and the HttpWebResponse class are in the &quot;System.Net&quot; namespace.



MORE INFORMATION
To send an e-mail message by using WebDAV in Visual C#, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Console Application.

Note In Visual Studio 2005, click Console Application in the Visual C# list.

In Visual Studio .NET, Class1.cs 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 description for Class1. /// class Class1 { static void Main(string[] args) {  try {   // TODO: Replace with the name of the computer that is running Exchange 2000. string strServer = &quot;ExchServe&quot;; // TODO: Replace with the sender's alias. string strSenderAlias = &quot;sender&quot;; // TODO: Replace with the sender's e-mail address. string strFrom = &quot;sender@example.com&quot;; // TODO: Replace with the recipient's e-mail address. string strTo = &quot;recipient@example.com&quot;; string strSubject = &quot;Send Using HttpWebRequest&quot;; string strBody = &quot;Hello World&quot;;

string sUri; sUri = &quot;http://&quot; + strServer + &quot;/Exchange/&quot; + strSenderAlias; sUri = sUri + &quot;/%23%23DavMailSubmissionURI%23%23/&quot;;

System.Uri myUri = new System.Uri(sUri); HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

string sQuery; DateTime mySentTime = new DateTime; sQuery = &quot;From: &quot; + strFrom + &quot;\n&quot; + &quot;To: &quot; + strTo + &quot;\n&quot; + &quot;Subject: &quot; + strSubject + &quot;\n&quot; + &quot;Date: &quot; + DateTime.Now.ToString + &quot;\n&quot; + &quot;X-Mailer: My DAV mailer&quot; + &quot;\n&quot; + &quot;MIME-Version: 1.0&quot; + &quot;\n&quot; + &quot;Content-Type: text/plain;&quot; + &quot;\n&quot; + &quot;Charset = \&quot;iso-8859-1\&quot;&quot; + &quot;\n&quot; + &quot;Content-Transfer-Encoding: 7bit&quot; + &quot;\n&quot; + &quot;\n&quot; + strBody;

// Set the credentials. // TODO: Replace with the appropriate user credential. NetworkCredential myCred = new NetworkCredential(@&quot;DomainName\User&quot;, &quot;Password&quot;); CredentialCache myCredentialCache = new CredentialCache; myCredentialCache.Add(myUri, &quot;Basic&quot;, myCred); HttpWRequest.Credentials = myCredentialCache;

// Set the headers. HttpWRequest.Headers.Set(&quot;Translate&quot;, &quot;f&quot;); HttpWRequest.ContentType = &quot;message/rfc822&quot;; HttpWRequest.ContentLength = sQuery.Length;

//Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000; // Set the request method. HttpWRequest.Method = &quot;PUT&quot;;

// Store the data in a byte array. byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery); HttpWRequest.ContentLength = ByteQuery.Length; Stream QueryStream = HttpWRequest.GetRequestStream; // write the data to be posted to the Request Stream QueryStream.Write(ByteQuery,0,ByteQuery.Length); QueryStream.Close;

// Send the request and get the response. HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse;

// Get the Status code. int iStatCode = (int)HttpWResponse.StatusCode; string sStatus = iStatCode.ToString; Console.WriteLine(&quot;Status Code: {0}&quot;, 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(&quot;Response: {0}&quot;, sText);

// Close the stream. strm.Close;

// Clean up. myCred = null; myCredentialCache = null; HttpWRequest = null; HttpWResponse = null; QueryStream = null; strm = null; sr = null; }  catch (Exception e)   { Console.WriteLine(&quot;{0} Exception caught.&quot;, 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 e-mail message was sent and received.</li></ol>

Additional query words: HttpWebRequest,HttpWebResponse,System.Net,send,e-mail

Keywords: kbmsg kbhowto KB313128

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.