'.SendUsingAccount selection for .onmicrosoft.com custom domain account

I set up a custom domain for my Microsoft 365 business account ("@company.com" is the custom domain which is really "@company.onmicrosoft.com").

I have two other accounts, which go through other mail servers like "@yahoo.com", "@gmail.com".

I created a VBA sub to send email and use .SendUsingAccount to send with different accounts as needed.

The function works for the "@yahoo.com", "@gmail.com" accounts but when I select my "@Company.com" (default) account, which is basically a "@company.onmicrosoft.com" account, it selects the immediately following "@yahoo.com" account.

Here's the code:

For i = 1 To OlApp.Session.Accounts.Count
    If OlApp.Session.Accounts.Item(i).SmtpAddress = "[email protected]" Then AccNo = i
    ' Debug.Print "Acc name: " & OlApp.Session.Accounts.Item(i) & " Acc number: " & i & " , email: " & OlApp.Session.Accounts.Item(i).SmtpAddress
Next i
Set objOlAccount = OlApp.Session.Accounts.Item(i)
Set OlMail = OlApp.CreateItem(olMailItem)
Set OlMail.SendUsingAccount = objOlAccount

When I run Debug.Print I see the "[email protected] account for i=1, the "@yahoo.com" account for i=2, "@Gmail.com" account for i=3.

I went as far as forcing the selection: Set objOlAccount = OlApp.Session.Accounts.Item(1), but my "@yahoo.com" account is still used to send the email.

How can I set the send account to my "@company.com" ("@company.onmicrosft.com") account?



Solution 1:[1]

Set objOlAccount = OlApp.Session.Accounts.Item(i)

should be

Set objOlAccount = OlApp.Session.Accounts.Item(AccNo)

Might be better as:

For i = 1 To OlApp.Session.Accounts.Count
    If OlApp.Session.Accounts.Item(i).SmtpAddress = "[email protected]" Then
        Set objOlAccount = OlApp.Session.Accounts.Item(i)
        Exit For
    End If
Next i

If Not objOlAccount Is Nothing then
    Set OlMail = OlApp.CreateItem(olMailItem)
    Set OlMail.SendUsingAccount = objOlAccount
End If

Solution 2:[2]

When I added my [email protected] account I used IMAP settings. Microsoft exchange server added a second [email protected] account, through exchange, without me realizing I had two such accounts. Once I removed the [email protected] account I used IMAP settings, all was sorted.

Solution 3:[3]

If you are sending through an Exchange account in Outlook, it always uses the default SMTP address of that account.

To work around that, you'd need to send using straight SMTP (it preserves the right sender address if you are using one of the proxy addresses). On the end user level, you can use a utility like Proxy Manager (I am its author).

If it is just a matter of "@company.com" vs "@company.onmicrosoft.com", you need to set your "@company.com" address as the default one in the Exchange admin console.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Williams
Solution 2 Piet L
Solution 3