Microsoft KB Archive/314187

= How to send a simple e-mail by using XMLHTTP and WebDAV in Visual Basic .NET =

Article ID: 314187

Article Last Modified on 4/29/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q314187



INTRODUCTION
This article describes how to programmatically send a simple e-mail message by using the XMLHTTP object and the Web Distributed Authoring and Versioning (WebDAV) protocol in Microsoft Visual Basic .NET. The sample code in the &quot;More Information&quot; section performs the following steps:
 * 1) Creates an XMLHTTP object.
 * 2) Calls the Open method of the XMLHTTP object to open the mail submission Uniform Resource Identifier (URI) of the user.
 * 3) Builds up a request string, and then puts the mail headers and the body message in the request string.
 * 4) Sends the request string to the Exchange server by using the WebDAV PUT method.



MORE INFORMATION
To programmatically send a simple e-mail message, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Visual Basic Projects types, click Console Application.

By default, Module1.vb is created. Add a reference to Microsoft XML 3.0 or to Microsoft XML 4.0. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, click Microsoft XML v3.0 or Microsoft XML v4.0, and then click Select. In the Add References dialog box, click OK to accept your selections.</li> Click Yes if you receive a prompt to generate wrappers for the libraries that you selected.</li></ol> </li>  In the code window, replace the existing code with the following code: Module Module1

Sub Main ' To use MSXML 3.0, use the following Dim statements: Dim xmlReq As MSXML2.XMLHTTP30 = New MSXML2.XMLHTTP30

' To use MSXML 4.0, use the following Dim statements: 'Dim xmlReq As MSXML2.XMLHTTP40 = New MSXML2.XMLHTTP40

' TODO: Replace with your Sender and Recipient information. Dim strSenderAlias As String = &quot;UserAlias&quot; Dim strFrom As String = &quot;UserAlias@microsoft.com&quot; Dim strTo As String = &quot;to@microsoft.com&quot; Dim strSubject As String = &quot;Send Using Webdav&quot; Dim strBody As String = &quot;Hello World&quot;

Dim sUri As String 'TODO: Replace ExchServer with the name of your Exchange Server. sUri = &quot;http://ExchServer/Exchange/&quot; & strSenderAlias & &quot;/##DavMailSubmissionURI##&quot;

'TODO: Replace with the domain, the username, and the password for the sender. xmlReq.open(&quot;PUT&quot;, sUri, False, &quot;UserDomain\UserAlias&quot;, &quot;UserPassword&quot;)

Dim mySentTime As DateTime = New DateTime Dim strReq As String strReq = &quot;From: &quot; & strFrom & vbNewLine & _ &quot;To: &quot; & strTo & vbNewLine & _ &quot;Subject: &quot; & strSubject & vbNewLine & _ &quot;Date: &quot; & DateTime.Today.Now & vbNewLine & _ &quot;X-Mailer: My DAV mailer&quot; & vbNewLine & _ &quot;MIME-Version: 1.0&quot; & vbNewLine & _ &quot;Content-Type: text/plain&quot; & vbNewLine & _ &quot;Charset = &quot;&quot;iso-8859-1&quot;&quot;&quot; & vbNewLine & _ &quot;Content-Transfer-Encoding: 7bit&quot; & vbNewLine & vbNewLine & _ strBody

xmlReq.setRequestHeader(&quot;Translate&quot;, &quot;f&quot;) xmlReq.setRequestHeader(&quot;Content-Type&quot;, &quot;message/rfc822&quot;) xmlReq.setRequestHeader(&quot;Content-Length&quot;, &quot;&quot; & strReq.Length)

xmlReq.send(strReq)

Console.WriteLine(xmlReq.status) Console.WriteLine(xmlReq.statusText) Console.WriteLine(xmlReq.responseText)

xmlReq = Nothing End Sub

End Module </li> Search for &quot;TODO&quot; in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Verify that the e-mail message has been sent and received.</li></ol>

<div class="references_section">