HOWTO: How to aSend Message On Behalf Of Another User
ID: Q239568
|
The information in this article applies to:
-
Collaboration Data Objects (CDO), versions 1.2, 1.21
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 (or higher) 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 = "<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
- 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.
Additional query words:
alternate active messaging
Keywords : kbCDO kbCDO120 kbCDO121 kbMsg kbVBp kbGrpMsg kbDSupport
Version : WINDOWS:1.2,1.21
Platform : WINDOWS
Issue type : kbhowto