简体   繁体   中英

Forward an email received in shared mailbox, setting FROM to a specific user account

I receive invoices from suppliers that I want to forward to a QBO (Quickbooks Online) email address that handles 'receipts/invoices' wherein it reads the attachments and parses the info within - this speeds up data entry.

QBO only accepts these emails from specific email addresses (ie, ones that are registered as accounts in QBO). So assume that QBO only accepts emails from "me@mydomain.com". I receive the invoices on "billing@mydomain.com" which is a shared Office 365 mailbox that "me@mydomain.com" has access to.

My VBA code should forward the currently selected emails (found within the billing@mydomain.com mailbox) using the me@mydomain.com sending address to mydomain@qbodocs.com.

The problem is that the forwarded email arrives in the recipient's mailbox as having come from billing@mydomain.com. When I .Display (instead of .Send ), I see that the sending account is set "correctly" yet it still arrives from the wrong account.

I decided that after the window pops up (using .Display ), I would change the sending account to something else, then back to the intended sending account - and it works. So there's some setting/headers other than .SentOnBehalfOfName that I need to set as I don't want any user intervention.

Option Explicit
    
Public Sub SendToQBO()
    Dim Email As Object
    Dim Sender As String
    Sender = "me@mydomain.com"
      
    For Each Email In ActiveExplorer.Selection
        With Email.Forward
            ' Just send to myself for now until this is figured out
            .To = Sender
            '.To = "mydomain@qbodocs.com"
            .Subject = "Sent From Outlook"
            .Body = Email.Body
          
            .SendUsingAccount = Session.Accounts(Sender)
            .SentOnBehalfOfName = Sender
            .Send
            ' Using .Display instead shows the right sending address, but it's ineffective
            ' unless I select another, then select it again before manually sending.
            ' .Display
        End With
    Next
End Sub

There is no need to set up two properties simultaneously on the mail item.

The MailItem.SendUsingAccount returns or sets an Account object that represents the account under which the MailItem is to be sent. The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called. Note, corresponding account should be configured in Outlook:

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

In case given permissions to the shared account you can use the MailItem.SentOnBehalfOfName property which allows setting a string indicating the display name for the intended sender of the mail message. In that case the account may not be configured in Outlook.

And the last bit which I've noticed in your code:

For Each Email In ActiveExplorer.Selection

Remember, the selection object may contain different kind of Outlook items - appointments, posts, mail items and etc. So, I'd suggest adding a check for the item type before accessing MailItem methods and properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM