Microsoft KB Archive/239568

= How to send a message on behalf of another user =

Article ID: 239568

Article Last Modified on 9/8/2005

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 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
 Create a new project (Standard EXE) in Microsoft Visual Basic. Add a project reference to the Microsoft CDO (1.2, 1.21) Library. Add a command button to the default form.  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 = " " 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 ("",,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  Set the names of the recipient and OnBehalf sender to appropriate values. Run the project. A message should be sent to the recipient with the OnBehalf sender listed as the sender.</li></ol>

Additional query words: alternate active messaging

Keywords: kbhowto kbmsg KB239568

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.