0
votes

I am trying send several items at the same time.

I have two mail addresses in my Outlook. The first is a personal work mail (like [email protected]) and second is a shared mail account (like [email protected]).

I tried:

Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
Dim oAccount As Outlook.account
Set oAccount = Session.accounts.Item(2)
objMailMessage.SendUsingAccount = oAccount
objMailMessage.display

I found this won't work with shared email accounts.

Most forums advise to use .SentOnBehalfOfName. When I try it on one email, the email has something like this in the Sender box [email protected] - Sent on behalf of name: "[email protected].

When I send mail from Outlook manually, it only shows the shared account on the received message. (It is not a big deal, but would be nicer if the code would work in the same style as manually.)

Also, when I put the code in a loop, half mails are sent with shared accounts and half with personal account.

And here is the code with .SentOnBehalfOfName which is going to be looped.

Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
With objMailMessage
    .To = email
    .Subject = msgSubj
    .CC = ccp
    .BCC = "[email protected]"
    .SentOnBehalfOfName = "[email protected]"
    .HTMLBody = msgText & "<br>" & "<br>" & msgSign
    .Attachments.Add path
    If rev > 0 Then
        .Save
    Else
        .Send
    End If
End With
1
What error do you get with the first code? The second code does not show enough to support " half mails are sent with shared accounts and half with personal account. minimal reproducible example - niton
As I remember, it does not see the second account, instead of error it just uses the only available (personal) account. When I tried to loop over Session.accounts.Items printing Item names, it only showed the private account. However, when in Outlook I change the shared account to any other, like my second personal account, it works. - Alex Shangin
If it does not see the second account you may have added a mailbox. To add an account. support.office.com/en-us/article/… - niton
I cannot add the second account separately, It is a shared account and it is added automatically when i add my personal work account. And i can send using that account from outlook itself, but not from vba - Alex Shangin

1 Answers

1
votes

This code attempts to ensure the .SentOnBehalfOf is consistent.

Option Explicit

Sub sendFromOtherMailbox()

    ' Code in Outlook, not called from another application

    Dim objMailMessage As mailItem
    Dim uMailbox As recipient

    ' Should not be necessary but this is used later to ensure
    ' the entry in .SentOnBehalfOfName is what you think it is.
    Set uMailbox = Session.CreateRecipient("Preferably display name here rather than email address.")

    uMailbox.Resolve

    ' An email address always resolves so the "If Resolved" test is not useful
    '  if an email address was used in .CreateRecipient
    If uMailbox.Resolved Then

        Set objMailMessage = CreateItem(olMailItem)

        With objMailMessage

            .Subject = "send From Other Mailbox"
            .SentOnBehalfOfName = uMailbox
            .Display

        End With

    End If

End Sub