0

场景:

两个团队:MainTeamHelpingTeam

MainTeam 独占使用共享邮箱,宏“代表MainTeam ”发送所有电子邮件,而不是作为共享邮箱发送。

HelpingTeam用户将帮助其他团队。他们需要表明电子邮件是“代表MainTeam ”发送的。

共享邮箱已添加到HelpingTeam上的用户,在新的邮件窗口中,共享邮箱的电子邮件地址低于他们的个人邮箱地址。使用这个“发件人”地址将表明他们正在尝试发送邮箱,这是我们不想要的。

我可以向他们展示如何添加另一个“发件人”地址并将其设置为使用他们的主帐户到“SendonBehalfOf”,但他们不想混淆,因为现在他们将在“发件人”列表中看到两个条目:“ SendAs”条目(固定,不能删除)和“SendonBehalfOf”条目(可以删除)。

我正在尝试更改电子邮件属性,以便代表共享邮箱发送电子邮件。

  • 使用此宏从共享邮箱发送电子邮件时,一切正常。
  • 从个人邮箱发起电子邮件并将发件人更改为“SendAs”帐户(列表中唯一的共享帐户)时,宏中的属性似乎正确,但 Outlook 不处理更改,系统拒绝信息。

我做了很多修改,以至于我忘记了哪些有效,哪些无效。以下是如上所述的最实用的版本。MsgBox条目是为了帮助我跟踪幕后发生的事情:

Dim oAccount As Outlook.Account
Dim objItem As MailItem
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objMailItem As Outlook.MailItem
Dim WithEvents myOlExp As Outlook.Explorer
Dim Sender As Outlook.AddressEntry

Private Sub Application_Startup()
    Initialize_handler
End Sub

Public Sub Initialize_handler()
    Set objInspectors = Application.Inspectors
    Set myOlExp = Application.ActiveExplorer
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olMail Then
        Set objMailItem = Inspector.CurrentItem
        If objMailItem.Sent = False Then
            Call SetFromAddress(objMailItem)
        End If
    End If
End Sub

Public Sub SetFromAddress(objMailItem As Outlook.MailItem)
    'To see which account user is trying to send from
    MsgBox "[SetFromAddress] SendUsingAccount: " & objMailItem.SendUsingAccount
    MsgBox "[SetFromAddress] SentOnBehalfOfName: " & objMailItem.SentOnBehalfOfName
    
    'Check which account is in focus as primary
    If objMailItem.SendUsingAccount = "MainTeam@company.com" Then
        MsgBox "sendfromaddress if triggered"
        'set sender to be the Shared Mailbox
        objMailItem.SentOnBehalfOfName = "MainTeam@company.com"
          
        'Find Primary O365 account and use that to send the email "on behalf of"
        For Each oAccount In Application.Session.Accounts
            If oAccount = "U.ser@company.com" Then
                objMailItem.SendUsingAccount = oAccount
            End If
        Next
    End If
    
    MsgBox "SetFromAddress Sending As: " & objMailItem.SendUsingAccount
    MsgBox "SetFromAddress OnBehalf: " & objMailItem.SentOnBehalfOfName
End Sub

'Below enables Outlook 2013/2016/365 Reading Pane Reply
Private Sub myOlExp_InlineResponse(ByVal objItem As Object)
    Call SetFromAddress(objItem)
End Sub

'Added the sub below in case the user manually switchs from personal to shared mailbox
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    MsgBox "[ItemSend] SendUsingAccount: " & Item.SendUsingAccount
    MsgBox "[ItemSend] SentOnBehalfOfName: " & Item.SentOnBehalfOfName
    
    'Check if Shared Account
    If Item.SentOnBehalfOfName = "MainTeam@company.com" Then
        MsgBox "If triggered"
        'set sender to be the Shared Mailbox
        Item.SentOnBehalfOfName = "MainTeam@company.com"
        
        'Find Primary O365 account and use that to send the email "on behalf of"
        For Each oAccount In Application.Session.Accounts
            If oAccount = "U.ser@company.com" Then
                Item.SendUsingAccount = oAccount
            End If
        Next
    End If
    
    MsgBox "[ItemSend] SendUsingAccount: " & Item.SendUsingAccount
    MsgBox "[ItemSend] SentOnBehalfOfName: " & Item.SentOnBehalfOfName
End Sub

2020 年 11 月 30 日

这就是我现在解决这个问题的方式,但如果它是一个在线回复,它会失败:

Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
    Dim oAccount As Outlook.Account
    Dim objItem As MailItem
 
    'To test later which account user is trying to send from
    Set SendingAccount = item.SendUsingAccount
        
    'Check if Shared Account
    If SendingAccount = "MainTeam@company.com" Then

        'Intecept email, stop it from sending, and create a new one "on behalf of"
        If TypeOf item Is MailItem Then
            Set objItem = item.Copy
            item.Delete
            Cancel = True
                       
            'set sender to be the Shared Mailbox
            objItem.SentOnBehalfOfName = "MainTeam@company.com"
            
            'Find Primary O365 account and use that to send the email "on behalf of"
            For Each oAccount In Application.Session.Accounts
                If oAccount = "U.ser@company.com" Then
                    objItem.SendUsingAccount = oAccount
                End If
            Next
        End If
        
        'send email
        objItem.Send
    End If
End Sub
4

2 回答 2

1

看起来我找到了一个很好的解决方法!虽然不是答案,它至少使这段代码工作。我基本上发送了检查名称的命令SendKeys "%k"(ALT+k),它同时检查发件人和收件人字段。当 CTRL+k 检查新邮件的名称时,它会在回复时打开插入超链接窗口,这就是我使用 ALT+k 的原因。

我在SetFromAddress的末尾和检查正确发送帐户的 for 语句中添加了这个。我在 for 语句的内部和外部都进行了测试,但内部每次都有效。

Public Sub SetFromAddress(objMailItem As Outlook.MailItem)
    'To see which account user is trying to send from
    
    'Check which account is in focus as primary
    If objMailItem.SendUsingAccount = "MainTeam@company.com" Then
        'set sender to be the Shared Mailbox
        objMailItem.SentOnBehalfOfName = "MainTeam@company.com"
          
        'Find Primary O365 account and use that to send the email "on behalf of"
        For Each oAccount In Application.Session.Accounts
            If oAccount = "U.ser@company.com" Then
                objMailItem.SendUsingAccount = oAccount
            End If
        Next
    End If
    SendKeys "%k
End Sub

            For Each oAccount In Application.Session.Accounts
                If oAccount = "U.ser@company.com" Then
                    objItem.SendUsingAccount = oAccount
                    sendkeys (%k)
                End If
            Next
        End If

它并不完美,但它现在可以工作,直到我能弄清楚如何处理内联响应。

于 2020-12-09T14:48:05.917 回答
0

我必须欺骗 Outlook 接受SentOnBehalfOfName. 您的设置可能会有所不同。

Dim oAccount As account

Const mailAddressShared = "MainTeam@company.com"

Private Sub setSentOnBehalfName()
    Dim currItem As MailItem
    Set currItem = ActiveInspector.currentItem
    Debug.Print currItem.subject
    currItem.SentOnBehalfOfName = mailAddressShared
    currItem.Save
End Sub

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Debug.Print "[ItemSend] SendUsingAccount: " & Item.SendUsingAccount
    Debug.Print "[ItemSend] SentOnBehalfOfName: " & Item.SentOnBehalfOfName
    
    Dim copiedItem As Object
    
    'Check if Shared Account
    If Item.SentOnBehalfOfName = mailAddressShared Then
    
        ' trick Outlook into accepting .SentOnBehalfOfName
        Set copiedItem = Item.Copy
        
        'assign shared mailbox
        copiedItem.SentOnBehalfOfName = mailAddressShared
        Debug.Print "copiedItem.SentOnBehalfOfName: " & copiedItem.SentOnBehalfOfName
    
    ElseIf Item.SentOnBehalfOfName = "" Then
    
        If MsgBox("Assign shared mailbox to SentOnBehalfOfName?", vbYesNo) = vbYes Then
        
            ' trick Outlook into accepting .SentOnBehalfOfName
            Set copiedItem = Item.Copy
            
            'assign shared mailbox
            copiedItem.SentOnBehalfOfName = mailAddressShared
            Debug.Print "copiedItem.SentOnBehalfOfName: " & copiedItem.SentOnBehalfOfName
            
        End If
        
    End If
    
    'Find default account to send the email
    If Not copiedItem Is Nothing Then
    
        Item.Delete
        Cancel = True   ' cancels original item
        
        For Each oAccount In Session.Accounts
            If oAccount = Session.GetDefaultFolder(olFolderInbox).Parent Then
                copiedItem.SendUsingAccount = oAccount
                Exit For
            End If
        Next
        
        Debug.Print "[ItemSend] copiedItem.SendUsingAccount: " & copiedItem.SendUsingAccount
        Debug.Print "[ItemSend] copiedItem.SentOnBehalfOfName: " & copiedItem.SentOnBehalfOfName
    
        copiedItem.Send ' does not re-trigger ItemSend
        
    Else
    
        Debug.Print "[ItemSend] Item.SendUsingAccount: " & Item.SendUsingAccount
        Debug.Print "[ItemSend] Item.SentOnBehalfOfName: " & Item.SentOnBehalfOfName
        
        For Each oAccount In Session.Accounts
            If oAccount = Session.GetDefaultFolder(olFolderInbox).Parent Then
                Item.SendUsingAccount = oAccount
                Exit For
            End If
        Next
    
        Debug.Print "[ItemSend] Item.SendUsingAccount: " & Item.SendUsingAccount
    End If
    
End Sub
于 2020-11-27T19:16:48.233 回答