Sending a Message (WebDAV)
Topic Last Modified: 2009-07-27
The following example uses the WebDAV PUT method and MOVE Method to create and send an RFC 822-formatted e-mail message. The PUT method creates the message in the drafts folder of the sender's inbox and the MOVE Method is used to send the message by moving it to the DAV mail submission Uniform Resource Identifier (URI) of the recipient.
It is also possible to use the WebDAV PUT method to save the message to the DAV mail submission URI directly, but this results in the received Field on the message not being set. If the received Field is not set, POP3 clients may not be able to download the message from the Exchange server. Saving the message to the drafts folder of the sender's inbox and moving it to the DAV mail submission URI will set the received Field properly.
See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.
This topic contains Microsoft® Visual Basic®, Microsoft C#, and Visual Basic .NET code examples.
Example
[Visual Basic]
Option Explicit
Private Sub SendMessage()
Dim strSubURI As String
Dim strTempURI As String
Dim strAlias As String
Dim strUserName As String
Dim strPassWord As String
Dim strExchSvrName As String
Dim strTo As String
Dim strSubject As String
Dim strBody As String
Dim strText As String
Dim bRequestSuccessful As Boolean
' To use MSXML 3.0, use the following Dim statements.
Dim xmlReq As IXMLHTTPRequest
Dim xmlReq2 As IXMLHTTPRequest
' To use MSXML 4.0, use the following Dim statements.
' Dim xmlReq As MSXML2.XMLHTTP40
On Error GoTo ErrHandler
' Exchange server name.
strExchSvrName = "Server"
' Alias of the sender.
strAlias = "alias"
' User name of the sender.
strUserName = "Domain\alias"
' Password of the sender.
strPassWord = "!Password"
' E-mail address of the sender.
strTo = "alias2"
' Subject of the mail.
strSubject = "DAV Message Test"
' Text body of the mail.
strBody = "This message was sent using WebDAV"
' Build the submission URI. If Secure Sockets Layer (SSL)
' is set up on the server, use "https://" instead of "http://".
strSubURI = "http://" & strExchSvrName & "/exchange/" & _
strAlias & "/%23%23DavMailSubmissionURI%23%23/"
' Build the temporary URI. If SSL is set up on the
' server, use "https://" instead of "http://".
strTempURI = "http://" & strExchSvrName & "/exchange/" & _
strAlias & "/drafts/" & strSubject & ".eml"
' Construct the 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.
strText = "To: " & strTo & vbNewLine & _
"Subject: " & strSubject & vbNewLine & _
"Date: " & 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
' Initialize.
bRequestSuccessful = False
' To use MSXML 3.0, use the following Set statement.
Set xmlReq = CreateObject("Microsoft.XMLHTTP")
' To use MSXML 4.0, use the following Set statement.
' Set xmlReq = CreateObject("Msxml2.XMLHTTP.4.0")
' Open the request object with the PUT method and
' specify that it will be sent asynchronously. The
' message will be saved to the drafts folder of the
' specified user's inbox.
xmlReq.open "PUT", strTempURI, False, strUserName, strPassWord
' Set the Content-Type header to the RFC 822 message format.
xmlReq.setRequestHeader "Content-Type", "message/rfc822"
' Send the request with the message body.
xmlReq.send strText
' The PUT request was successful.
If (xmlReq.Status >= 200 And xmlReq.Status < 300) Then
bRequestSuccessful = True
' An error occurred on the server.
ElseIf (xmlReq.Status = 500) Then
MsgBox "PUT request status:" & xmlReq.Status & vbCrLf & _
"Status text: An error occurred on the server."
GoTo ErrExit
Else
MsgBox "PUT request status:" & xmlReq.Status & vbCrLf & _
"Status text: " & xmlReq.statusText
GoTo ErrExit
End If
' If the PUT request was successful,
' MOVE the message to the mailsubmission URI.
If bRequestSuccessful Then
' To use MSXML 3.0, use the following Set statement.
Set xmlReq2 = CreateObject("Microsoft.XMLHTTP")
' To use MSXML 4.0, use the following Set statement.
' Set xmlReq2 = CreateObject("Msxml2.XMLHTTP.4.0")
' Open the request object with the PUT method and
' specify that it will be sent asynchronously.
xmlReq2.open "MOVE", strTempURI, False, strUserName, strPassWord
xmlReq2.setRequestHeader "Destination", strSubURI
xmlReq2.send
' The MOVE request was successful.
If (xmlReq2.Status >= 200 And xmlReq2.Status < 300) Then
MsgBox "Message was sent to " & strTo & " successfully."
' An error occurred on the server.
ElseIf (xmlReq.Status = 500) Then
MsgBox "MOVE request status:" & xmlReq.Status & vbCrLf & _
"Status text: An error occurred on the server."
GoTo ErrExit
Else
MsgBox "MOVE request status:" & xmlReq.Status & vbCrLf & _
"Status text: " & xmlReq.statusText
GoTo ErrExit
End If
End If
' Clean up.
Set xmlReq = Nothing
Set xmlReq2 = Nothing
Exit Sub
ErrHandler:
' Display error information.
MsgBox Err.Number & ": " & Err.Description
' Clean up.
Set xmlReq = Nothing
Set xmlReq2 = Nothing
Exit Sub
ErrExit:
' Clean up.
Set xmlReq = Nothing
Set xmlReq2 = Nothing
End Sub
Example
using System;
using System.Net;
using System.IO;
using System.Text;
namespace ExchangeSDK.Snippets.CSharp
{
class SendingMessageWebDAV
{
[STAThread]
static void Main(string[] args)
{
System.Net.HttpWebRequest PUTRequest;
System.Net.WebResponse PUTResponse;
System.Net.HttpWebRequest MOVERequest;
System.Net.WebResponse MOVEResponse;
System.Net.CredentialCache MyCredentialCache;
string strMailboxURI = "";
string strSubURI = "";
string strTempURI = "";
string strServer = "ServerName";
string strPassword = "!Password";
string strDomain = "Domain";
string strAlias = "SenderAlias";
string strTo = "recipient@example.com";
string strSubject = "WebDAV message test.";
string strText = "This message was sent using WebDAV.";
string strBody = "";
byte[] bytes = null;
System.IO.Stream PUTRequestStream = null;
try
{
// Build the mailbox URI.
strMailboxURI = "http://" + strServer + "/exchange/" + strAlias;
// 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 + "/exchange/" + strAlias
+ "/##DavMailSubmissionURI##/";
// Build the temporary URI for the message. If SSL is set
// up on the server, use "https://" instead of "http://".
strTempURI = "http://" + strServer + "/exchange/" + strAlias
+ "/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 + "\n" +
"Subject: " + strSubject + "\n" +
"Date: " + System.DateTime.Now +
"X-Mailer: test mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/plain;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 7bit" + "\n" +
"\n" + strText;
// 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 HttpWebRequest object.
PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
// Add the network credentials to the request.
PUTRequest.Credentials = MyCredentialCache;
// Specify the PUT method.
PUTRequest.Method = "PUT";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)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 = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
// Create the HttpWebRequest object.
MOVERequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
// 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 = (System.Net.HttpWebResponse)MOVERequest.GetResponse();
Console.WriteLine("Message successfully sent.");
// Clean up.
PUTResponse.Close();
MOVEResponse.Close();
}
catch(Exception ex)
{
// 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);
}
}
}
}
Visual Basic .NET
Example
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 = "ServerName"
strPassword = "!Password"
strDomain = "Domain"
strAlias = "SenderAliase"
strTo = "recipient@example.com"
strSubject = "WebDAV message test."
strText = "This message was sent using WebDAV."
' Build the mailbox URI.
strMailboxURI = "http://" & strServer & "/exchange/" & strAlias
' 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 & "/exchange/" & strAlias _
& "/##DavMailSubmissionURI##/"
' Build the temporary URI for the message. If SSL is set
' up on the server, use "https://" instead of "http://".
strTempURI = "http://" & strServer & "/exchange/" & strAlias & _
"/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 & strText
' 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