Sunday, February 12, 2006

WebDAV for .Net Sample Application

This is a sample to send email using WebDAV protocol:

'define public variables
Private strUsername As String
Private strPassword As String
Private strAddress As String
Private strServer As String
Private strDomain As String
Private strMailboxURI As String

Public Sub SendEmail(ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String)

'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 strMailboxURI As String
Dim strSubURI As String
Dim strTempURI As String
Dim PUTRequestStream As System.IO.Stream
Dim bytes() As Byte
Try
' Build the mailbox URI.
strMailboxURI = "http://" & strServer & "/exchange/" & strAddress
' Build the submission URI for the message. If Secure
' Sockets Layer (SSL) is set up on the server, use
' "https://" instead of "http://".
strSubURI = strMailboxURI & "/##DavMailSubmissionURI##/"
' Build the temporary URI for the message. If SSL is set
' up on the server, use "https://" instead of "http://".
strTempURI = strMailboxURI & "/drafts/" & 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 = "To: " & strTo & vbNewLine & _
"Subject: " & strSubject & vbNewLine & _
"Date: " & System.DateTime.Now & _
"X-Mailer: test mailer" & vbNewLine & _
"MIME-Version: 1.0" & vbNewLine & _
"Content-Type: text/plain;" & vbNewLine & _
"Charset = ""iso-8859-1""" & vbNewLine & _
"Content-Transfer-Encoding: 7bit" & vbNewLine & _
vbNewLine & strBody
' Create the PUT HttpWebRequest object.
PUTRequest = CType(System.Net.WebRequest.Create(strTempURI), _
System.Net.HttpWebRequest)
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
' Add the network credentials to the request.
Dim myCredential As System.Net.NetworkCredential = New System.Net.NetworkCredential(strUsername, strPassword, strDomain)
PUTRequest.Credentials = myCredential
' 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 = myCredential
' 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
Console.WriteLine(ex.Message)
End Try

End Sub

No comments: