繁体   English   中英

VBA 从非默认账户发送 Email

[英]VBA Send Email From Non-Default Account

我使用两个 Outlook 帐户登录,我需要发送一封未设置为默认帐户的电子邮件。

我尝试使用.SendOnBehaulfOfName ,但即使它在发送 email 之前向我显示了正确的帐户,但在我发送之后我看到它是从默认的 email 发送的(这是错误的)。 我也尝试使用.SendUsingAccount但它没有更改“发件人”email。

    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim ol As Outlook.Application
    Set ol = New Outlook.Application
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)

    With OutlookMail
        .To = lista_envio
        .CC = ""
        .BCC = ""
        .Subject = assunto_email
        .Display '
        .HTMLBody = mensagem
        .Attachments.Add (caminho)
        .SendUsingAccount = ol.Session.Accounts("Contato")
    End With

    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    Set ol = Nothing

使用For each Account in Outlook.Application.Session.Accounts方法也不起作用。

关于什么是错误的任何想法?

谢谢

确保在 Outlook 中配置了这样的帐户。如果您想在 Outlook UI 上看到更改,您需要在通过 Outlook object model 设置所有属性后调用Display方法。

如果在 Outlook 中配置了其他帐户(不仅仅是一个额外的商店),您需要选择MailItem.SendUsingAccount属性,该属性设置一个Account object,表示要在其下发送MailItem的帐户。 您可以找到以下演示如何使用它的示例代码:

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

SendOnBehaulfOfName属性不需要在 Outlook 中配置的帐户。只需要在 Exchange 中代表另一个人发送的权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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