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 = "ExchServer"
' TODO: Replace with the sender's alias.
Dim strSenderAlias As String = "sender"
' TODO: Replace with the sender's e-mail address.
Dim strFrom As String = "sender@example.com"
' TODO: Replace with the recipient's e-mail address.
Dim strTo As String = "recipient@example.com"
Dim strSubject As String = "Send Using HttpWebRequest"
Dim strBody As String = "Hello World"
Dim sUri As String
sUri = "http://" & strServer & "/Exchange/" & strSenderAlias & _
"/%23%23DavMailSubmissionURI%23%23/"
Dim myUri As System.Uri = New System.Uri(sUri)
Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)
Dim sQuery As String
sQuery = "From: " & strFrom & vbNewLine & _
"To: " & strTo & vbNewLine & _
"Subject: " & strSubject & vbNewLine & _
"Date: " & DateTime.Now.ToString() & vbNewLine & _
"X-Mailer: My DAV mailer" & vbNewLine & _
"MIME-Version: 1.0" & vbNewLine & _
"Content-Type: text/plain" & vbNewLine & _
"Charset = ""iso-8859-1""" & vbNewLine & _
"Content-Transfer-Encoding: 7bit" & vbNewLine & vbNewLine & _
strBody
' TODO: Replace with the appropriate user credentials.
Dim myCred As NetworkCredential = New NetworkCredential("Domain\UserName", "Password")
Dim MyCredentialCache As CredentialCache = New CredentialCache()
MyCredentialCache.Add(myUri, "Basic", myCred)
HttpWRequest.Credentials = MyCredentialCache
' Set the headers.
HttpWRequest.Headers.Set("Translate", "f")
HttpWRequest.ContentType = "message/rfc822"
HttpWRequest.ContentLength = sQuery.Length
'Set the request timeout to 5 minutes.
HttpWRequest.Timeout = 300000
' set the request method
HttpWRequest.Method = "PUT"
' 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("Status: {0} {1}", sStatus, HttpWResponse.StatusDescription.ToString())
Console.WriteLine("Request Headers:")
Console.WriteLine(HttpWRequest.Headers.ToString())
Console.WriteLine("Response Headers:")
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("Response: {0}", sText)
' Close the stream.
strm.Close()
' Clean up
HttpWRequest = Nothing
HttpWResponse = Nothing
MyCredentialCache = Nothing
myCred = Nothing
strm = Nothing
sr = Nothing
End Sub
End Module
|