Option Explicit On
Option Strict On
Module Module1
Public Sub Main()
' Variables.
Dim PUTRequest As System.Net.HttpWebRequest
Dim PUTResponse As System.Net.HttpWebResponse
Dim MOVERequest As System.Net.HttpWebRequest
Dim MOVEResponse As System.Net.HttpWebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim strMailboxURI As String
Dim strSubURI As String
Dim strTempURI As String
Dim strServer As String
Dim strPassword As String
Dim strDomain As String
Dim strAlias As String
Dim strTo As String
Dim strSubject As String
Dim strText As String
Dim strBody As String
Dim PUTRequestStream As System.IO.Stream
Dim bytes() As Byte
Try
' Initialize variables.
strServer = "192.168.0.1"
strPassword = "-"
strDomain = "techdomain"
strAlias = "Administrator"
strTo = "testfolder@techdomain.com"
strSubject = "WebDAV message test"
strText = "This message was sent using WebDAV"
' Build the mailbox URI.
strMailboxURI = "http://" & strServer & "/public/testfolder/"
' Build the submission URI for the message. If Secure
' Sockets Layer (SSL) is set up on the server, use
' "https://" instead of "http://".
'strSubURI = "http://" & strServer & "/public/testfolder/" & "/##DavMailSubmissionURI##/"
strSubURI = "http://" & strServer & "/public/testfolder/" & "123/Test.eml"
' Build the temporary URI for the message. If SSL is set
' up on the server, use "https://" instead of "http://".
strTempURI = "http://" & strServer & "/public/testfolder/" & _
strSubject & ".eml"
' Construct the RFC 822 formatted body of the PUT request.
' Note: If the From: header is included here,
' the MOVE method request will return a
' 403 (Forbidden) status. The From address will
' be generated by the Exchange server.
strBody = "From: Some One <someone@example.com>" & vbNewLine & _
"MIME-Version: 1.0" & vbNewLine & _
"Content-Type: multipart/mixed;" & vbNewLine & _
" boundary = ""XXXXboundary text""" & vbNewLine & _
vbNewLine & _
"This is a multipart message in MIME format." & vbNewLine & _
vbNewLine & _
"--XXXXboundary text" & vbNewLine & _
"Content-Type: text/plain" & vbNewLine & _
vbNewLine & _
"this is the body text 2" & vbNewLine & _
vbNewLine & _
"--XXXXboundary text" & vbNewLine & _
"Content-Type: text/plain;" & vbNewLine & _
"Content-Disposition: attachment;" & vbNewLine & _
" filename = ""test.txt""" & vbNewLine & _
vbNewLine & _
"this is the attachment text" & vbNewLine & _
vbNewLine & _
"--XXXXboundary text--" & vbNewLine
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strMailboxURI), _
"NTLM", _
New System.Net.NetworkCredential(strAlias, strPassword, strDomain) _
)
' Create the PUT HttpWebRequest object.
PUTRequest = CType(System.Net.WebRequest.Create(strTempURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
PUTRequest.Credentials = MyCredentialCache
' Specify the PUT method.
PUTRequest.Method = "PUT"
' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(strBody)
' Set the content header length. This must be
' done before writing data to the request stream.
PUTRequest.ContentLength = bytes.Length
' Get a reference to the request stream.
PUTRequestStream = PUTRequest.GetRequestStream()
' Write the message body to the request stream.
PUTRequestStream.Write(bytes, 0, bytes.Length)
' Close the Stream object to release the connection
' for further use.
PUTRequestStream.Close()
' Set the Content-Type header to the RFC 822 message format.
PUTRequest.ContentType = "message/rfc822"
' PUT the message in the Drafts folder of the
' sender's mailbox.
PUTResponse = CType(PUTRequest.GetResponse(), System.Net.HttpWebResponse)
Console.WriteLine("Message successfully PUT to " & strTempURI)
' Create the MOVE HttpWebRequest object.
MOVERequest = CType(System.Net.WebRequest.Create(strTempURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
MOVERequest.Credentials = MyCredentialCache
' Specify the MOVE method.
MOVERequest.Method = "MOVE"
' Set the Destination header to the
' mail submission URI.
MOVERequest.Headers.Add("Destination", strSubURI)
' Send the message by moving it from the Drafts folder of the
' sender's mailbox to the mail submission URI.
MOVEResponse = CType(MOVERequest.GetResponse(), System.Net.HttpWebResponse)
Console.WriteLine("Message successfully sent to " & strTo)
' Clean up.
PUTResponse.Close()
MOVEResponse.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the PUT
' or MOVE method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module