How to send a message on behalf of another user (239568)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q239568

SUMMARY

There may be circumstances when a message should be sent with a different user listed as the sender. This can be accomplished by setting the Sender property of the message object to the desired user.

MORE INFORMATION

Sometimes a message needs to be programmatically created and sent with a different user - other than the current user - listed as the sender. This is also referred to as sending "on behalf of" another user.

By default the Sender property of the Message object is the CurrentUser of the MAPI Session object. Setting the Sender property to a different AddressEntry object will cause the message to be sent as if that other user had sent it.

The actual sender (CurrentUser) must be listed as a delegate of the "on behalf of" sender for the code sample below to work properly. Administrative policies on the messaging server may also prevent this code from working.

The following sample code demonstrates sending a message on behalf of somebody else.

Warning Any use by you of the code provided in this article is at your own risk. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Step-by-step example

  1. Create a new project (Standard EXE) in Microsoft Visual Basic.
  2. Add a project reference to the Microsoft CDO (1.2, 1.21) Library.
  3. Add a command button to the default form.
  4. Add the following code to the form module:
    Option Explicit
    
    Sub Command1_Click()
       Dim oSession As MAPI.Session
       Dim MsgNew As MAPI.Message  'uses early binding
       Dim Recip As MAPI.Recipient
       Dim AddEntries As MAPI.AddressEntries
       Dim OnBehalfSender As MAPI.AddressEntry
    
       Set oSession = CreateObject("mapi.session")
       oSession.Logon     'use existing session
    
       'create new message
       Set MsgNew = oSession.Outbox.Messages.Add
    
       'set on behalf sender
       Set AddEntries = oSession.AddressLists(1).AddressEntries
       AddEntries.Filter = Nothing    'reset
       'TODO: Change on behalf user name
       AddEntries.Filter.name = "<joe user>"
       Set OnBehalfSender = AddEntries.GetFirst
       Set MsgNew.Sender = OnBehalfSender  'set on behalf address
       Set MsgNew.Sender = oSession.CurrentUser  'optional, the actual sender
    
       'set message recipient
       'TODO: Change recipient name
       Set Recip = MsgNew.Recipients.Add ("<Mary Otheruser>",,cdoTo)
       Recip.Resolve
    
       'set other message properties and send
       With MsgNew
          .Text = "Message body"
          .Subject = "Test mesage"
          .Update    'optional, leaves unsent mail in Outbox if Send fails
          .Send
       End With
    
       'release objects
       Set MsgNew = Nothing
       Set OnBehalfSender = Nothing
       Set Recip = Nothing
       Set AddEntries = Nothing
       oSession.Logoff
       Set oSession = Nothing
    
    End Sub
    					
  5. Set the names of the recipient and OnBehalf sender to appropriate values.
  6. Run the project. A message should be sent to the recipient with the OnBehalf sender listed as the sender.

Modification Type:MajorLast Reviewed:9/8/2005
Keywords:kbhowto kbMsg KB239568