【问题标题】:Forward an email received in shared mailbox, setting FROM to a specific user account转发共享邮箱中收到的电子邮件,将 FROM 设置为特定用户帐户
【发布时间】:2021-09-10 05:36:35
【问题描述】:

我收到来自供应商的发票,我想将这些发票转发到 QBO(Quickbooks Online)电子邮件地址,该地址处理“收据/发票”,其中读取附件并解析其中的信息 - 这加快了数据输入。

QBO 仅接受来自特定电子邮件地址(即在 QBO 中注册为帐户的电子邮件)的这些电子邮件。所以假设 QBO 只接受来自“me@mydomain.com”的邮件。我在“billing@mydomain.com”上收到发票,这是“me@mydomain.com”可以访问的共享 Office 365 邮箱。

我的 VBA 代码应该使用 me@mydomain.com 发送地址将当前选择的电子邮件(在 billing@mydomain.com 邮箱中找到)转发到 mydomain@qbodocs.com。

问题是转发的电子邮件到达收件人的邮箱时就像来自 billing@mydomain.com。当我.Display(而不是.Send)时,我看到发送帐户设置“正确”但它仍然来自错误的帐户。

我决定在弹出窗口后(使用.Display),我会将发送帐户更改为其他帐户,然后返回到预期的发送帐户 - 它可以工作。因此,我需要设置除 .SentOnBehalfOfName 之外的一些设置/标题,因为我不希望任何用户干预。

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

【问题讨论】:

    标签: vba outlook office365


    【解决方案1】:

    邮件项无需同时设置两个属性。

    MailItem.SendUsingAccount 返回或设置一个Account 对象,该对象表示将在其下发送MailItem 的帐户。 SendUsingAccount 属性可用于指定在调用Send 方法时应该用于发送MailItem 的帐户。注意,需要在 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
    

    如果授予共享帐户的权限,您可以使用MailItem.SentOnBehalfOfName 属性,该属性允许设置一个字符串,指示邮件的预期发件人的显示名称。在这种情况下,帐户可能未在 Outlook 中配置。

    我在您的代码中注意到的最后一点:

    For Each Email In ActiveExplorer.Selection
    

    请记住,选择对象可能包含不同类型的 Outlook 项目 - 约会、帖子、邮件项目等。因此,我建议在访问 MailItem 方法和属性之前添加对项目类型的检查。

    【讨论】:

    • 问题是,由于 billing@mydomain.com 是一个共享邮箱,Outlook 中只有一个可以通过 VBA 访问的实际帐户,即 me@mydomain.com。因此,即使考虑到只有一个帐户可以进行发送,它仍然被报告为来自 billing@ 地址。我认为通常这是可取/可配置的行为,只是在这种情况下不是。
    • 另外仅供参考,这是一个 IMAP/Exchange 帐户,而不是 POP3。
    • 此时不需要使用MailItem.SendUsingAccount 属性。 SentOnBehalfOfName 在 Exchange 配置文件中有意义。
    • 我也尝试过仅使用该属性,但问题仍然存在。我相信最终这是因为转发的电子邮件来自 billing@ 邮箱,因此 Exchange 中有某种设置,每当电子邮件从该邮箱发出时,发件人允许 FROM 地址显示为来自共享帐户,而不是来自实际发件人。我可以通过生成全新的电子邮件而不是使用 Email.Forward 来规避这个问题。如果可行,我会发布解决方案。
    猜你喜欢
    • 2018-11-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多