【问题标题】:How to send mail from shared email account?如何从共享电子邮件帐户发送邮件?
【发布时间】:2018-10-01 00:48:39
【问题描述】:

我正在尝试同时发送多个项目。

我的 Outlook 中有两个邮件地址。第一个是个人工作邮件(如 j.doe@company.com),第二个是共享邮件帐户(如 support@company.com)。

我试过了:

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

我发现这不适用于共享电子邮件帐户

大多数论坛建议使用.SentOnBehalfOfName。当我在一封电子邮件上尝试时,电子邮件的发件人框中有类似这样的内容j.doe@company.com - Sent on behalf of name: "support@company.com

当我手动从 Outlook 发送邮件时,它只在收到的邮件上显示共享帐户。 (这没什么大不了的,但如果代码能够以与手动相同的方式工作会更好。)

另外,当我将代码放入循环中时,一半的邮件使用共享帐户发送,一半使用个人帐户发送

这是带有.SentOnBehalfOfName的代码,它将被循环。

Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
With objMailMessage
    .To = email
    .Subject = msgSubj
    .CC = ccp
    .BCC = "support@company.com"
    .SentOnBehalfOfName = "support@company.com"
    .HTMLBody = msgText & "<br>" & "<br>" & msgSign
    .Attachments.Add path
    If rev > 0 Then
        .Save
    Else
        .Send
    End If
End With

【问题讨论】:

  • 第一个代码有什么错误?第二个代码显示不够,无法支持“一半邮件使用共享帐户发送,一半使用个人帐户发送。minimal reproducible example
  • 我记得,它没有看到第二个帐户,而不是错误它只是使用唯一可用的(个人)帐户。当我尝试遍历 Session.accounts.Items 打印项目名称时,它只显示私人帐户。但是,当我在 Outlook 中将共享帐户更改为任何其他帐户时,例如我的第二个个人帐户,它可以工作。
  • 如果没有看到第二个帐户,您可能已经添加了一个邮箱。添加帐户。 support.office.com/en-us/article/…
  • 我不能单独添加第二个账号,它是共享账号,添加个人工作账号时会自动添加。我可以从 Outlook 本身使用该帐户发送,但不能从 vba 发送

标签: vba email outlook mailitem


【解决方案1】:

此代码尝试确保 .SentOnBehalfOf 是一致的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2012-09-10
    相关资源
    最近更新 更多