HOWTO: WebDAV: Send mail with Custom Form and set properties
This is going to be a very simple sample of how you can send an email with custom properties set and also change the message class so that it render on outlook as Custom Form.
Prerequisites: A custom form must be published with same class name to render the email properly.
How it works…
Step 1) It drop an email in the drafts folder of user
Step 2) Patches the properties to set Message Class & other properties, even including with special characters in name like Spaces and ‘/’
Step 3) Drop the mail to DavMailSubmissionURL to send it
Code:
Imports System.Net
Imports System.IO
Imports System.Web
Module Module1
Const strServer As String = "Exchange_Server_Name"
Const strMailbox As String = "Target_Mailbox"
Const strFrom As String = "from@domain.com"
Const strTo As String = "to@domain.com"
Const strSubject As String = "Custom form email - sent using WebDAV"
Const strBody As String = "<B>Hello There,</B><BR> Hope you are doing good.<BR>Good Bye!!!"
Const strMessageClass As String = "IPM.Note.TestMessageClass"
Const strDomain As String = "DomainNameHere"
Const strUsername As String = "UsernameToLoginWith"
Const strPassword As String = "Password"
Dim objCDO As New CDO.Message
Dim strMIME As String
Dim sQuery As String = ""
Dim sUri As Uri = New Uri("https://" & strServer & "/Exchange/" + strMailbox & "/Drafts/" & Guid.NewGuid().ToString() & ".eml")
Dim sDavSubmissionURI As Uri = New Uri("https://" & strServer & "/Exchange/" + strMailbox & "/%23%23DavMailSubmissionURI%23%23/")
Dim DavRequest As HttpWebRequest = DirectCast(WebRequest.Create(sUri), HttpWebRequest)
Dim myCred As New NetworkCredential((strDomain & "\" & strUsername), strPassword)
Dim myCredentialCache As New CredentialCache()
Dim ByteQuery As Byte()
Dim QueryStream As Stream
Dim DavResponse As HttpWebResponse
Dim iStatCode As Integer
Dim sStatus As String
Dim strm As Stream
Dim sr As StreamReader
Dim sText As String
Sub Main()
Console.WriteLine("Preparing message...")
PrepairMessage()
Console.WriteLine("Creating message in draft folder...")
CreateMessageInDraft()
Console.WriteLine("Updating message class & other properties...")
SetMessageProperties()
Console.WriteLine("Sending message...")
SendMessage()
Console.WriteLine("Message Sent")
End Sub
Sub PrepairMessage()
With objCDO
.From = strFrom
.To = strTo
.Subject = strSubject
.HTMLBody = strBody
strMIME = .GetStream.ReadText
End With
sQuery = strMIME
End Sub
Sub CreateMessageInDraft()
myCredentialCache.Add(sUri, "Basic", myCred)
DavRequest.Credentials = myCredentialCache
DavRequest.KeepAlive = False
DavRequest.Headers.Set("Translate", "f")
DavRequest.ContentType = "message/rfc822"
DavRequest.ContentLength = sQuery.Length
'Set the request timeout to 5 minutes.
DavRequest.Timeout = 300000
' Set the request method.
DavRequest.Method = "PUT"
' Store the data in a byte array.
ByteQuery = System.Text.Encoding.UTF8.GetBytes(sQuery)
DavRequest.ContentLength = ByteQuery.Length
QueryStream = DavRequest.GetRequestStream()
' write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
QueryStream.Close()
QueryStream = Nothing
' Send the request and get the response.
DavResponse = DirectCast(DavRequest.GetResponse(), HttpWebResponse)
' Get the Status code.
iStatCode = CInt(DavResponse.StatusCode)
sStatus = iStatCode.ToString()
Console.WriteLine("Status Code: " & sStatus.ToString())
' Read the response stream.
strm = DavResponse.GetResponseStream()
sr = New StreamReader(strm)
sText = sr.ReadToEnd()
Console.WriteLine("Response: " & sText)
' Close the stream.
strm.Close()
End Sub
Sub SetMessageProperties()
Dim sPropPatch As String = "<?xml version=""1.0""?>" + _
"<g:propertyupdate xmlns:g=""DAV:"">" + _
"<g:set>" + _
"<g:prop>" + _
"<d:outlookmessageclass xmlns:d=""https://schemas.microsoft.com/exchange/"">" & strMessageClass & "</d:outlookmessageclass>" + _
"<NewCustomProperty>" & "CustomProperty1" & "</NewCustomProperty>" + _
"</g:prop>" + _
"</g:set>" + _
"</g:propertyupdate>"
DavRequest = Nothing
DavResponse = Nothing
DavRequest = DirectCast(WebRequest.Create(sUri), HttpWebRequest)
DavRequest.Credentials = myCredentialCache
DavRequest.KeepAlive = False
DavRequest.Headers.Set("Translate", "f")
DavRequest.ContentType = "text/xml"
DavRequest.ContentLength = sQuery.Length
'Set the request timeout to 5 minutes.
DavRequest.Timeout = 300000
' Set the request method.
DavRequest.Method = "PROPPATCH"
' Store the data in a byte array.
ByteQuery = System.Text.Encoding.UTF8.GetBytes(sPropPatch)
DavRequest.ContentLength = ByteQuery.Length
QueryStream = DavRequest.GetRequestStream()
' write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
QueryStream.Close()
QueryStream = Nothing
' Send the request and get the response.
DavResponse = DirectCast(DavRequest.GetResponse(), HttpWebResponse)
' Get the Status code.
iStatCode = CInt(DavResponse.StatusCode)
sStatus = iStatCode.ToString()
Console.WriteLine("Status Code: " & sStatus.ToString())
' Read the response stream.
strm = DavResponse.GetResponseStream()
sr = New StreamReader(strm)
sText = sr.ReadToEnd()
Console.WriteLine("Response: " & sText)
' Close the stream.
strm.Close()
DavResponse = Nothing
DavRequest = Nothing
End Sub
Sub SendMessage()
DavRequest = Nothing
DavResponse = Nothing
DavRequest = DirectCast(WebRequest.Create(sUri), HttpWebRequest)
DavRequest.Credentials = myCredentialCache
'Set the request timeout to 5 minutes.
DavRequest.Timeout = 300000
' Set the request method.
DavRequest.Method = "MOVE"
DavRequest.Headers.Add("Destination", sDavSubmissionURI.ToString())
' Send the request and get the response.
DavResponse = DirectCast(DavRequest.GetResponse(), HttpWebResponse)
' Get the Status code.
iStatCode = CInt(DavResponse.StatusCode)
sStatus = iStatCode.ToString()
Console.WriteLine("Status Code: " & sStatus.ToString())
' Read the response stream.
strm = DavResponse.GetResponseStream()
sr = New StreamReader(strm)
sText = sr.ReadToEnd()
Console.WriteLine("Response: " & sText)
' Close the stream.
strm.Close()
DavResponse = Nothing
DavRequest = Nothing
End Sub
End Module
Note: I will update the sample to include custom properties with special characters soon… last edited: 5/23/09 4:19 AM
Comments
Anonymous
May 22, 2009
PingBack from http://asp-net-hosting.simplynetdev.com/howto-webdav-send-mail-with-custom-form-and-set-properties/Anonymous
June 03, 2009
You can use the following property sets to update a Custom property with spaces and special characters in it. <?xml version="1.0"?> <g:propertyupdate xmlns:g="DAV:"> <g:set> <g:prop> <d:outlookmessageclass xmlns:d="http://schemas.microsoft.com/exchange/">IPM.Note.IA</d:outlookmessageclass> <Report_x0020_Date_x0020_Time b:dt="dateTime.tz" xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" >2009-06-03T19:16:25Z</Report_x0020_Date_x0020_Time> <m:_x0020_Suspect xmlns:m="Victim /">victim / suspect</m:_x0020_Suspect> <m:_x0020_Charge xmlns:m="Crime /">crime / charge</m:_x0020_Charge> <Officer_x0020_ID>officer id</Officer_x0020_ID> <Case_x0020_Number>case number</Case_x0020_Number> <Incident_x0020_Number>incident number</Incident_x0020_Number> <CaseRecordID>12345678</CaseRecordID> </g:prop> </g:set> </g:propertyupdate>