0

I have been struggling to programmatically send an email as a Shared Mailbox and NOT on behalf of.

I have tried this code that I can't remember now from where I took it, did a few modifications to it.

Public Sub test()

    Dim outApp As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Recipient
    Dim Recipients As Recipients
    Dim addrEntry As Outlook.AddressEntry
    Dim addrEntries As Outlook.AddressEntries
    Dim nameSpace As Outlook.nameSpace
    Dim addrLists As Outlook.AddressLists
    Dim uMailInbox As Outlook.Recipient
     
    Set outApp = CreateObject("Outlook.Application")
    Set objOutlookMsg = outApp.CreateItem(olMailItem)
    Set nameSpace = outApp.GetNamespace("MAPI")
    Set addrLists = nameSpace.Session.AddressLists
    
    Set addrEntry = addrLists.Item("Global Address List").AddressEntries.Item("testSender")

    Set Recipients = objOutlookMsg.Recipients
    Set objOutlookRecip = Recipients.Add("testReceiver@testdomain.com")
    objOutlookRecip.Type = 1
    
    objOutlookMsg.Sender = addrEntry
    
'    Debug.Print objOutlookMsg.SentOnBehalfOfName
    
    objOutlookMsg.Subject = "Testing this macro"
    objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
    
    For Each objOutlookRecip In objOutlookMsg.Recipients
        objOutlookRecip.Resolve
    Next
    
    objOutlookMsg.Display
    objOutlookMsg.Send
    
    Set outApp = Nothing

End Sub

I have also given the following permissions to the account used on the outlook app: -Read and manage permissions -Send as permissions

And purposely not given the permission: -Send on behalf of permission

Still the received email has the quote "sent on behalf of"

A second approach suggested adding the account of the Shared Mailbox to the outlook accounts and then send the email using the second account corresponding to the Shared Mailbox. However using this approach I still got the "sent on behalf of" quote, even though all this time I didn't have the "Send on behalf of permission"

Finally a third approach here suggested creating the email item from the folder outlook of the Shared Mailbox.

Public Sub test2()

    Dim outApp As Outlook.Application
    Dim trgtStore As Outlook.Store
    Dim trgtFolder As Outlook.Folder
    Dim emailItem As Outlook.MailItem
    Dim recip As Outlook.Recipient
    Dim addrEntry As Outlook.AddressEntry
    Dim addrLists As Outlook.AddressLists
    Dim nameSpace As Outlook.nameSpace
    
    Set outApp = CreateObject("Outlook.Application")
    Set trgtStore = outApp.Session.Stores("testSender")
    
    Set trgtFolder = trgtStore.GetDefaultFolder(4) ' olFolderOutbox = 4
    Set emailItem = trgtFolder.Items.Add
    
    Set nameSpace = outApp.GetNamespace("MAPI")
    Set addrLists = nameSpace.Session.AddressLists
    
    Set addrEntry = addrLists.Item("Global Address List").AddressEntries.Item("testSender")
    
    With emailItem
        
        Set recip = .Recipients.Add("testReceiver@testdomain.com")
        recip.Type = 1 'olTo = 1  olOriginator = 0 olCC = 2 olBCC = 3
        .Subject = "Testing this macro"
        .HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
        .Sender = addrEntry
        .Display
        .Send
        
    End With
    
End Sub

Every time I get the "sent on behalf of" quote on the received email... Can anyone help with this issue please?

Best regards

David
  • 38
  • 6
  • Confirm the shared mailbox is set up as an account recognized by VBA with `Sub ShowAllAccounts()` here https://stackoverflow.com/questions/62729844/switching-the-from-inbox/62731358#62731358 – niton Nov 21 '22 at 17:17

1 Answers1

1

The Sender property is read-only, so you can't set it in the following way:

objOutlookMsg.Sender = addrEntry

Instead, you can use two possible options:

  1. The MailItem.SentOnBehalfOfName property which returns a string indicating the display name for the intended sender of the mail message. Inthat case you need to make sure that you have the permissions to send on behalf of another person.
Set addrEntry = addrLists.Item("Global Address List").AddressEntries.Item("testSender")
    
    With emailItem
        
        Set recip = .Recipients.Add("testReceiver@testdomain.com")
        recip.Type = 1 'olTo = 1  olOriginator = 0 olCC = 2 olBCC = 3
        .Subject = "Testing this macro"
        .HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
        .SentOnBehalfOfName = addrEntry.Name
        .Display
        .Send
        
    End With
    
  1. The MailItem.SendUsingAccount property returns or sets an Account object that represents the account under which the MailItem is to be sent. Note, the account should be configured in Outlook. For example:
Sub SendUsingAccount() 
 Dim oAccount As Outlook.account 
 For Each oAccount In Application.Session.Accounts 
   If oAccount.AccountType = olPop3 Then 
     Dim oMail As Outlook.MailItem 
     Set oMail = Application.CreateItem(olMailItem) 
     oMail.Subject = "Sent using POP3 Account" 
     oMail.Recipients.Add ("someone@example.com") 
     oMail.Recipients.ResolveAll 
     Set oMail.SendUsingAccount = oAccount 
     oMail.Send 
   End If 
 Next 
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Hello Eugene, first of all thank you very much for your time and effort, it's very kind of you. However I have tried the 2 alternatives that you propose, but when the email is received by the destination email it says: "Current useraccount display name" on behalf of testSender, however I want the email to be sent as testSender and not on behalf of it... – David Nov 21 '22 at 15:11
  • You need to have another account configured in Outlook and use the `SendUsingAccount` property in the code to set up a proper account in that case. – Eugene Astafiev Nov 21 '22 at 16:31
  • Thanks Eugene, using the SendUsingAccount property allows me to send as using the Shared Mailbox BUT ONLY if the Shared Mailbox is previously installed/configured in the Outlook App. Another thing, the line 'Set oMail.SendUsingAccount = oAccount' should be changed to 'oMail.SendUsingAccount = oAccount' , otherwise you get a runtime error. – David Nov 22 '22 at 11:00
  • Isn't there a way to achieve the same result without having to install the shared mailbox account on the outlook account? Meaning that it will only use the fact that I gave permission to the user to -Read and manage permissions and -Send as permissions – David Nov 22 '22 at 11:03
  • In that case you need to use the `SentOnBehalfOfName` property. – Eugene Astafiev Nov 22 '22 at 18:07