Share via


Sending by Using Exchange

Topic Last Modified: 2006-06-30

When sending mail by using Microsoft® Exchange Server 2007, Collaboration Data Objects (CDO) uses the Exchange Mail Submission Uniform Resource Identifier (URI) for the user to transmit the message. This URI is available from the urn:schemas:httpmail:sendmsg property for the user's root private mailbox folder. When sending a message using Exchange, an OLE DB provider is used.

A special set of configuration properties can be set when sending messages by using Exchange. Each property in the following table is within the https://schemas.microsoft.com/cdo/configuration/ namespace unless denoted otherwise.

Property Name Description

mailboxurl

The URL to the user's Exchange mailbox. This URL can be in either the File: or HTTP: URL scheme format. If this property is not set, CDO looks up the user's mailbox URL by using the Active Directory® directory service and the user principal name (UPN) in the sendusername property.

activeconnection

An open Microsoft ActiveX® Data Objects (ADO) Connection object to the user's mailbox. If this property is set, the connection is used to look up the user's mail submission URI, to bind to the user's mail submission URI, and transmit the message. Set this property if you do not want a currently bound Message object to rebind to the user's mailbox when sending a message using Exchange. When messages are sent using Exchange, CDO normally binds to the user's mailbox, and the current DataSource binding is changed.

urlsource

What source the URL in the mailboxurl property is set to. Currently, the only supported value is cdoExchangeMailboxURL, which is the default.

sendusername

The user's user principle name. For example, mailto:user1@domain.tld or user1@domain.tld.

sendpassword

The user's password.

The following examples demonstrate sending messages by using Exchange.

Example

VBScript

Option Explicit

' Variables.
Dim sTo         'As String
Dim sFrom       'As String
Dim sSubject    'As String
Dim sText       'As String
Dim sMailboxURL 'As String
Dim Conn        'As New ADODB.Connection

' Create Person and Connection objects.
Set Conn = CreateObject("ADODB.Connection")

' Initialize variables.
sTo = "user@subdomain.example.com"
sFrom = sTo
sSubject = "Subject"
sText = "Text of message."
sMailboxURL = "file://./backofficestorage/subdomain.example.com/mbx/user/"

' Open the connection.
Conn.Provider = "ExOLEDB.DataSource"
Conn.Open sMailboxURL


' Call subprocedure 'SendUsingExchange'.
Call SendMailUsingExchange(sMailboxURL, sTo, sFrom, sSubject, sText)

' Call subprocedure 'SendMailWithConnectionTrans'.
Call SendMailWithConnectionTrans(sMailboxURL, Conn, sTo, sFrom, sSubject, sText)

' Call subprocedure 'SendMailWithConnection'.
Call SendMailWithConnection(sMailboxURL, Conn, sTo, sFrom, sSubject, sText)

' Clean up.
Conn.Close
Set Conn = Nothing


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SendMailUsingExchange(sMailboxURL, sTo, sFrom, sSubject, sText)
 ' Assuming type information has been imported into the script host.
     ' For example, <reference object="CDO.Message"/> in .wsf script file.

 Dim Msg  ' As CDO.Message
 Dim Config' As CDO.Configuration

 ' Create message and configuration objects.
    Set Msg = CreateObject("CDO.Message")
 Set Config = CreateObject("CDO.Configuration")

 ' Get the message configuration object.
    Set Config = Msg.Configuration

 ' Set the sendusing field to 'cdoSendUsingExchange'.
    Config("https://schemas.microsoft.com/cdo/configuration/sendusing") = 3

 ' Set the mailboxurl field to the specified mailbox URL.
    Config("https://schemas.microsoft.com/cdo/configuration/mailboxurl") = sMailboxURL

 ' Update the fields.
    Config.Fields.Update

 ' Set the To, From, Subject, and TextBody fields on the message.
    Msg.To = sTo
    Msg.From = sFrom
    Msg.Subject = sSubject
    Msg.TextBody = sText

 ' Send the message.
    Msg.Send

 wscript.echo "Calling SendMailWithConnection is successful!"

 ' Clean up.
 Set Msg = Nothing
 Set Config = Nothing
End Sub

Sub SendMailWithConnectionTrans(sMailboxURL, Conn, sTo, sFrom, sSubject, sText)
    ' Assuming type information has been imported into the script host.
    ' For example, <reference object="CDO.Message"/> in .wsf script file.

 Dim Msg  ' As CDO.Message
 Dim Config' As CDO.Configuration

 ' Create message and configuration objects.
    Set Msg = CreateObject("CDO.Message")
    Set Config = CreateObject("CDO.Configuration")

 ' Get the message configuration object.
    Set Msg.Configuration = Config

 ' Set the sendusing field to 'cdoSendUsingExchange'.
    Config("https://schemas.microsoft.com/cdo/configuration/sendusing") = 3  'cdoSendUsingExchange

 ' Set the mailboxurl field to the specified mailbox URL.
    Config("https://schemas.microsoft.com/cdo/configuration/mailboxurl") = sMailboxURL

 ' Set the mailboxurl field to the specified mailbox URL.
    Config("https://schemas.microsoft.com/cdo/configuration/activeconnection") = Conn

 ' Update the fields.
    Config.Fields.Update

 ' Set the To, From, Subject, and TextBody fields on the message.
    Msg.To = sTo
    Msg.From = sFrom
    Msg.Subject = sSubject
    Msg.TextBody = sText

 ' Send the message.
    Conn.BeginTrans
    Msg.Send
    Conn.CommitTrans

 wscript.echo "Calling SendMailWithConnectionTrans is successful!"

 ' Clean up.
 Set Msg = Nothing
 Set Config = Nothing
End Sub

Sub SendMailWithConnection(sMailboxURL, Conn, sTo, sFrom, sSubject, sText)
    ' Assume type information has been imported into the script host.
    ' For example, <reference object="CDO.Message"/> in .wsf script file.

 Dim Msg  ' As CDO.Message
 Dim Config' As CDO.Configuration

 ' Create message and configuration objects.
    Set Msg = CreateObject("CDO.Message")
    Set Config = CreateObject("CDO.Configuration")

 ' Get the message configuration object.
    Set Msg.Configuration = Config

 ' Set the sendusing field to 'cdoSendUsingExchange'.
    Config("https://schemas.microsoft.com/cdo/configuration/sendusing") = 3  'cdoSendUsingExchange

 ' Set the mailboxurl field to the specified mailbox URL.
    Config("https://schemas.microsoft.com/cdo/configuration/mailboxurl") = sMailboxURL

 ' Set the mailboxurl field to the specified mailbox URL.
    Config("https://schemas.microsoft.com/cdo/configuration/activeconnection") = Conn

 ' Update the fields.
    Config.Fields.Update

 ' Set the To, From, Subject, and TextBody fields on the message.
    Msg.To = sTo
    Msg.From = sFrom
    Msg.Subject = sSubject
    Msg.TextBody = sText

 ' Send the message.
    Msg.Send

 wscript.echo "Calling SendMailWithConnection is successful!"

 ' Clean up.
 Set Msg = Nothing
 Set Config = Nothing
End Sub

C++

// Import the type libraries.
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace
#import "d:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace

#include <stdio.h>
#include <iostream.h>


struct StartOle
{
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void dump_com_error(_com_error &e)
{
  cout<< "An error occurred." <<endl;
  cout<< "Error code: " << e.Error()<<endl;
  cout<< "Error message: " << e.ErrorMessage()<<endl;

}


void SendMailUsingExchange(_bstr_t sMailboxURL, _bstr_t sTo,
                           _bstr_t sFrom, _bstr_t sSubject,
                           _bstr_t sText)
{
  IMessagePtr       Msg(__uuidof(Message));
  IConfigurationPtr Config(__uuidof(Configuration));

  try
  {
    // Get the message configuration object.
    Config = Msg->Configuration;

    // Get the sendusing field.
    FieldsPtr Flds = Config->GetFields();
    FieldPtr Fld1 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/sendusing");

    // Set the sendusing field to cdoSendUsingExchange.
    Fld1->Value = "3";

    // Get the mailboxurl field, set it to the mailbox URL.
    FieldPtr Fld2 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/mailboxurl");
    Fld2->Value = sMailboxURL;

    // Update the fields.
    Flds->Update();

    // Set the To, From, Subject, and Text fields on the message.
    Msg->To = sTo;
    Msg->From = sFrom;
    Msg->Subject = sSubject;
    Msg->TextBody = sText;

    // Send the message.
    Msg->Send();

    cout<< "Message sent from SendMailUsingExchange function." << endl;

    // Clean up.
    Flds = NULL;
    Fld1 = NULL;
    Fld2 = NULL;
  }
  catch(_com_error &e)
  {
    cout<<"Error occurred in SendMailUsingExchange function."<<endl;
    dump_com_error(e);
  }
}


void SendMailWithConnectionTrans(_bstr_t sMailboxURL, _ConnectionPtr Conn,
                                 _bstr_t sTo, _bstr_t sFrom,
                                 _bstr_t sSubject, _bstr_t sText)
{
  IMessagePtr       Msg(__uuidof(Message));
  IConfigurationPtr Config(__uuidof(Configuration));

  try
  {
    // Open the connection to the mailbox.
    Conn->Provider = "Exoledb.DataSource";
    HRESULT hRes = Conn->Open(sMailboxURL,"","",0);

    if (Conn->State != adStateOpen)
    {
      cout<<"Attempt to open the connection failed."<<endl;
      return;
    }

    // Get the message configuration object.
    Config = Msg->Configuration;

    // Get the sendusing field.
    FieldsPtr Flds = Config->GetFields();
    FieldPtr Fld1 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/sendusing");

    // Set the sendusing field to cdoSendUsingExchange.
    Fld1->Value = "3";

    // Get the mailboxurl field, set it to the specified mailbox store URL.
    FieldPtr Fld2 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/mailboxurl");
    Fld2->Value = sMailboxURL;

    // Get the activeconnection field, set it to the connection.
    FieldPtr Fld3 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/activeconnection");
    Fld3->Value = "Conn";

    // Update the fields.
    Flds->Update();

    // Set the To, From, Subject, and TextBody fields on the message.
    Msg->To = sTo;
    Msg->From = sFrom;
    Msg->Subject = sSubject;
    Msg->TextBody = sText;

    // Send the message.
    Conn->BeginTrans();
    Msg->Send();
    Conn->CommitTrans();

    cout<< "Message sent from SendMailWithConnectionTrans function." << endl;

    // Close the connection.
    Conn->Close();

    // Clean up.
    Flds = NULL;
    Fld1 = NULL;
    Fld2 = NULL;
    Fld3 = NULL;
    Conn = NULL;
  }
  catch(_com_error &e)
  {
    cout<< "Error occurred in SendMailWithConnectionTrans function." << endl;
    dump_com_error(e);
  }

  if (Conn)
    if (Conn->State == adStateOpen)
    {
      Conn->Close();
      Conn = NULL;
    }
}

void SendMailWithConnection(_bstr_t sMailboxURL, _ConnectionPtr Conn,
                            _bstr_t sTo, _bstr_t sFrom, _bstr_t sSubject,
                            _bstr_t sText)
{
  IMessagePtr       Msg(__uuidof(Message));
  IConfigurationPtr Config(__uuidof(Configuration));

  try
  {

    // Open the connection to the mailbox.
    Conn->Provider = "Exoledb.DataSource";
    HRESULT hRes = Conn->Open(sMailboxURL,"","",0);

    if (Conn->State != adStateOpen)
    {
      cout<<"Attempt to open the connection failed."<<endl;
      return;
    }

    // Get the message configuration object.
    Config = Msg->Configuration;

    // Get the sendusing field.
    FieldsPtr Flds = Config->GetFields();
    FieldPtr Fld1 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/sendusing");

    // Set the sendusing field to cdoSendUsingExchange.
    Fld1->Value = "3";

    // Get the mailboxurl field, set it to the specified mailbox URL.
    FieldPtr Fld2 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/mailboxurl");
    Fld2->Value = sMailboxURL;

    // Get the activeconnection field, set it to the connection.
    FieldPtr Fld3 = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/activeconnection");
    Fld3->Value = "Conn";

    // Update the fields.
    Flds->Update();

    // Set the To, From, Subject, and TextBody fields on the message.
    Msg->To = sTo;
    Msg->From = sFrom;
    Msg->Subject = sSubject;
    Msg->TextBody = sText;

    // Send the message.
    Msg->Send();

    cout<< "Message sent from SendMailWithConnection function." << endl;

    // Close the connection.
    Conn->Close();

    // Clean up.
    Flds = NULL;
    Fld1 = NULL;
    Fld2 = NULL;
    Fld3 = NULL;
    Conn = NULL;
  }
  catch(_com_error &e)
  {
    cout<< "Error occurred in SendMailWithConnection function." << endl;
    dump_com_error(e);
  }

  if (Conn)
    if (Conn->State == adStateOpen)
    {
      Conn->Close();
      Conn = NULL;
    }
}

void main()
{
  // Variables.
  _bstr_t sTo;
  _bstr_t sFrom;
  _bstr_t sSubject;
  _bstr_t sText;
  _bstr_t sMailboxURL;

  // Initialize variables.
  sTo = "user@subdomain.example.com";
  sFrom = sTo;
  sSubject = "Subject";
  sText = "Text of message.";
  sMailboxURL = "file://./backofficestorage/subdomain.example.com/mbx/user/";

  _ConnectionPtr Conn(__uuidof(Connection));

  // Call function 'SendMailUsingExchange'.
  SendMailUsingExchange(sMailboxURL, sTo, sFrom, sSubject, sText);

  // Call function 'SendMailWithConnectionTrans'.
  SendMailWithConnectionTrans(sMailboxURL, Conn, sTo, sFrom, sSubject, sText);

  // Call function 'SendMailWithConnection'.
  SendMailWithConnection(sMailboxURL, Conn, sTo, sFrom, sSubject, sText);
}