我的用户将他们的个人邮箱作为他们的主帐户,并在他们的Outlook 2010客户端中配置了一个自动映射的共享邮箱。共享邮箱是Office 365共享邮箱,因此无法登录以将其设置为主帐户。
我正在尝试从共享帐户的地址启动一封新电子邮件。
下面是我尝试使用的VBA代码。我已允许在Outlook的信任中心设置中使用宏。
Public Sub New_Mail()
Dim oAccount As Outlook.Account
Dim oMail As Outlook.MailItem
For Each oAccount In Application.Session.Accounts
If oAccount = "sharedMailboxAddress@domain.tld" Then
Set oMail = Application.CreateItem(olMailItem)
oMail.SendUsingAccount = oAccount
oMail.Display
End If
Next
End Sub
使用以下带有SentOnBehalfOfName属性的代码从共享邮箱的地址启动一封新电子邮件。感谢德米特里·斯特雷布尔琴科为我指明了正确的方向。
Sub New_Mail()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.SentOnBehalfOfName = "sharedMailboxAddress@domain.tld"
.Display
End With
Set objMsg = Nothing
End Sub
代表邮箱将不在Namespace.Accounts
列表中。
请改为设置MailItem.SentOnBehalfOfName
属性。
什么在您的代码中不起作用?
使用smtpAddress
属性可以选择一个帐户。
示例功能:
Private Function GetAccountForEmailAddress(smtpAddress As String) As Outlook.account
Dim account As Outlook.account
For Each account In Application.Session.accounts
If LCase(account.smtpAddress) = LCase(smtpAddress) Then
Set GetAccountForEmailAddress = account
Exit Function
End If
Next account
MsgBox "No Account with SmtpAddress: " & smtpAddress & " exists!", vbCritical, "Oops!"
Set GetAccountForEmailAddress = Nothing
End Function
debug.print行将显示帐户。
Option Explicit
Public Sub New_Mail()
Dim oAccount As account
Dim oMail As mailItem
For Each oAccount In Session.Accounts
Debug.Print oAccount
If LCase(oAccount) = LCase("text copied from the immediate window") Then
Set oMail = CreateItem(olMailItem)
oMail.SendUsingAccount = oAccount
oMail.Display
End If
Next
ExitRoutine:
Set oMail = Nothing
End Sub
不是我遇到的问题,但这个问题和答案有帮助。我的问题是在使用多个帐户时从特定邮箱发送。唯一的答案是:
OutMail.SendUsingAccount = OutApp.Session.Accounts.Item("address@domain.com")