0

I have below code which changes sender field and also changes account from which the email is being sent. My account is authorized to send also from this email. Code works well on new emails and also works well replied emails, but does not work well on one of the accounts I have. I have 3 accounts (POP, @outlook.com and Exchange). If email has been previously received from an @outlook.com account (this account is also set as exchange account) or set to be sent from an @outlook.com account it does not do the job. For example: If I open new email, set sending account to XX@outlook.com and then run code it does not work. Similar if I reply to an email, which I have received from xx@outlook.com account the code does not work. Code works well if my email (replied or new) is previously set to be sent from my POP account or from my third-exchange account. The code does not work, if my email (replied or new) is previously set to be sent from the @outlook.com account. Why is the code not working on all accounts?

Sub ChangeSender()
    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector
    If oInspector Is Nothing Then
        MsgBox "No active inspector"
    Else
        Set NewMail = oInspector.CurrentItem
        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            NewMail.SentOnBehalfOfName = "test@test.com"
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "accounttest@accounttest.com" Then
                    NewMail.SendUsingAccount = oAccount
                End If
            Next
            NewMail.Display           
        End If
    End If
End Sub

In case previously set account is the POP one or the third-exchange account the code works well. In case previously set account is the xx@outlook.com account, the code does not work. In VBA I can see the whole code runs through, but there is no change on the line >>NewMail.SendUsingAccount = oAccount as is in case when working properly. This is my first VBA code, so maybe I am missing some basics.

EDIT: Here is a visual image of what I am trying to do with VBA. Same think as I do with several mouse clicks and some typing: Change the email FROM field and change the account from which I am sending email from. Image

EDIT2: Now I have tried to remove line .SentOnBehalfOfName altogether and the script again works only on POP account and does not work on outlook.com Exchange account. In other words: I left only the line .SendUsingAccount and if email is preset to POP account it changes account correctly, if email is preset to outlook.com exchange account it does not change to the Exchange accounttest@accounttest.com account. Any suggestion why some accounts can be changed to another one, some cannot be changed to another one?

EDIT3: Above is image how I am sending email by hand (mouse/keyboard) with from field to test@test.com and it is being sent through my account accounttest@accounttest.com. I am doing this successfully for several months. For the last 2 weeks I have been using partially successfully above VBA code to achieve the same. VBA code works great if before running the code my email from field is set to my POP account or my Exchange account (accounttest@accounttest.com). Above VBA code does not work in case if before running it, my email from field is set to XX@outlook.com account. There are obviously some restrictions which account can change to which with VBA. When changing by hand, there are no restrictions. I think SentOnBehalfOfName line is not a question any more (it is working great when I change account successfully). Maybe I should make a new question altogether without this line?

JohnT
  • 3
  • 3
  • Not sure that I correctly understand what you try doing after `Else`... If you want sending the mail using the account `"test@test.com"`, you should try (in iteration): `If oAccount.DisplayName = "test@test.com" Then` `Set NewMail.SendUsingAccount = oAccount`, followed by `NewMail.Send` (or not, letting the following `NewMail.Display`...) – FaneDuru Feb 02 '23 at 08:58
  • Please see the newly added picture of what I am trying to do. – JohnT Feb 03 '23 at 04:53
  • 1. Do you have all the mentioned email accounts **configured in Microsoft Outlook**? 2. What Microsoft Outlook version do you use? 3. What you show in the picture does not look as a an Outlook I know window... Is it Outlook? 4. Does the picture show an attempt to send a mail **directly from an Office application (Excel, for instance)**? – FaneDuru Feb 03 '23 at 11:54
  • 1. I have all 3 accounts set up in Outlook. 2. Outlook 2019 3. It is Outlook from Office professional Plus 2019. 4. Directly from Outlook. I have been sending emails from different account like this for more than a decade with different versions of Outlook. Always with mouse/keyboard. – JohnT Feb 04 '23 at 17:45

2 Answers2

0

You need to set either SentOnBehalfOfName or SendUsingAccount property, but not both unless you set an SendUsingAccount to an Exchange account and SentOnBehalfOfName to a GAL user name from that Exchange account.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Yes, I want to change both. Please check my image above (EDIT). What I am trying to do with VBA is what I do with a few mouse clicks and typing. Both emails are in the GAL username on that Exchange. – JohnT Feb 03 '23 at 04:45
  • Do both properties end up being set to the expected values? Can you verify in OutlookSpy (https://www.dimastr.com/outspy) - click Item button, select the SentOnBehalfOfName property, then SentOnBehalfOfName property and click Browse. – Dmitry Streblechenko Feb 03 '23 at 15:51
  • Yes. When code is working correctly, SentOnBehalfOfName property is set to test@test.com and SendUsingAccount property -> Browse links to account accounttest@accounttest.com. Email is sent correctly. I have added EDIT3 above. Thanks for outlookspy link. It looks like a very nice tool. – JohnT Feb 04 '23 at 17:24
  • So what is not set when it does not work? Does changing the order in which the properties are set (SentOnBehalfOfName vs SendUsingAccount) makes any difference? – Dmitry Streblechenko Feb 04 '23 at 18:01
  • I am a little confused now. Outlookspy says that my above code is working correctly and changing both SendUsingAccount and SentOnBehalfOfName in all cases. But in one case (with xx@outlook.com) the FROM field visually on the email does not change. It is still xx@outlook.com ?!? – JohnT Feb 04 '23 at 18:42
  • I tried to send an email like this (with Outlookspy showing correct values and my eyes seeing FROM field with wrong value) the email went out through the correct outbox, but was rejected by Exchange server ("You can't send a message on behalf of this user unless you have permission to do so"). It looks like there should be a third property value beside SentOnBehalfOfName and SendUsingAccount which I need to change. I should see with my eyes that FROM field has been changed. – JohnT Feb 04 '23 at 18:43
0

In the following code:

        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            NewMail.SentOnBehalfOfName = "test@test.com"
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "accounttest@accounttest.com" Then
                    NewMail.SendUsingAccount = oAccount
                End If
            Next
            NewMail.Display           
        End If

The SentOnBehalfOfName is set without making sure you deal with an Exchange account set for the MailItem.SendUsingAccount property. It seems the chosen account in Outlook (or the default one) doesn't have permissions for sending on behalf of another person. Just need to keep the order of setting properties.

So, let's assume the accounttest@accounttest.com account belongs to Exchange where you have sufficient permissions for sending on behalf of another person. The code should look like that then:

        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "accounttest@accounttest.com" Then
                    NewMail.SendUsingAccount = oAccount
                    ' when you deal with an Exchange account 
                    NewMail.SentOnBehalfOfName = "test@test.com"
                End If
            Next
            NewMail.Display           
        End If

You can't set the SentOnBehalfOfName for every account in Outlook, it does make sense only for Exchange with sufficient permissions. So, I'd suggest using the Account.AccountType property for checking the account type and differentiate cases where you can use one or the other properties.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Unfortunately above solution with changing from field after changing account does not work. I do not know why. I have tried it before several times. I nearly gave up, when by accident, I changed position of both and as I said before now is working at least on some accounts. It must be first .SentOnBehalfOfName and after followed by . SendUsingAccount in order to work. I have tried your solution again. Again, not working and I do not know why. – JohnT Feb 03 '23 at 04:31