Microsoft KB Archive/314199

= How to use the HttpWebRequest class and the HttpWebResponse class to send an e-mail message by using Visual Basic .NET =

Article ID: 314199

Article Last Modified on 4/12/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition

-



This article was previously published under Q314199



SUMMARY
This article describes how to use the HttpWebRequest class and the HttpWebResponse class from the &quot;System.Net&quot; namespace to send a simple e-mail message by using Microsoft Visual Basic .NET.



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

By default, the Module1.vb file is created.  In the code window, replace the code with the following: Imports System.Net Imports System.IO

Module Module1 Sub Main ' TODO: Replace with the name of the computer that is running Exchange 2000. Dim strServer As String = &quot;ExchServer&quot; ' TODO: Replace with the sender's alias. Dim strSenderAlias As String = &quot;sender&quot; ' TODO: Replace with the sender's e-mail address. Dim strFrom As String = &quot;sender@example.com&quot; ' TODO: Replace with the recipient's e-mail address. Dim strTo As String = &quot;recipient@example.com&quot; Dim strSubject As String = &quot;Send Using HttpWebRequest&quot; Dim strBody As String = &quot;Hello World&quot;

Dim sUri As String

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

Dim myUri As System.Uri = New System.Uri(sUri) Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)

Dim sQuery As String sQuery = &quot;From: &quot; & strFrom & vbNewLine & _ &quot;To: &quot; & strTo & vbNewLine & _ &quot;Subject: &quot; & strSubject & vbNewLine & _ &quot;Date: &quot; & DateTime.Now.ToString & 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

' TODO: Replace with the appropriate user credentials. Dim myCred As NetworkCredential = New NetworkCredential(&quot;Domain\UserName&quot;, &quot;Password&quot;) Dim MyCredentialCache As CredentialCache = 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. Dim ByteQuery As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery) HttpWRequest.ContentLength = ByteQuery.Length Dim QueryStream As Stream = 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. Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse

' Get the status and the headers. Dim iStatCode As Integer = HttpWResponse.StatusCode Dim sStatus As String = iStatCode.ToString Console.WriteLine(&quot;Status: {0} {1}&quot;, sStatus, HttpWResponse.StatusDescription.ToString)

Console.WriteLine(&quot;Request Headers:&quot;) Console.WriteLine(HttpWRequest.Headers.ToString) Console.WriteLine(&quot;Response Headers:&quot;) Console.WriteLine(HttpWResponse.Headers.ToString)

' Get the response stream. Dim strm As Stream = HttpWResponse.GetResponseStream

' Read the response stream. Dim sr As StreamReader = New StreamReader(strm) Dim sText As String = sr.ReadToEnd Console.WriteLine(&quot;Response: {0}&quot;, sText)

' Close the stream. strm.Close

' Clean up       HttpWRequest = Nothing HttpWResponse = Nothing MyCredentialCache = Nothing myCred = Nothing strm = Nothing sr = Nothing End Sub End Module  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>

Keywords: kbhowtomaster KB314199

-

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

© Microsoft Corporation. All rights reserved.