.SentOnBehalfOf and .SendUsingAccount on forward - vba

I go into a shared inbox and forward an email as myself, to my boss, using HTML formatting.
I wrote VBA code that works on every step except changing the From line from the shared mailbox email address to my email address.
Let's call my personal email address "gwyn#email.com" and the shared mailbox email address "office#email.com". If I manually click on the From field I can select my own email address and send it.
Public Sub ForwardIAF()
Dim OutApp As Object
Dim OutAccount As Outlook.Account
Dim myinspector As Outlook.Inspector
Dim myIAF As Outlook.MailItem
Dim myForward As Outlook.MailItem
Set myIAF = GetCurrentItem()
Set myForward = myIAF.Forward
Set OutApp = CreateObject("Outlook.Application")
Set OutAccount = OutApp.Session.Accounts.Item(1)
With myForward
.SentOnBehalfOfName = "gwyn#email.com"
Debug.Print "myForward.SentOnBehalfOfName:" & "x " & myForward.SentOnBehalfOfName & " x"
.Recipients.Add "gwyn#email.com"
.BodyFormat = olFormatHTML
.Display
End With
End Sub
When the forward opens, it shows my email address in the From line, but when I send, it reverts to the office email address. The debug print shows my email address is in the .SentOnBehalfOf field, so it looks like it's there until it sends.
Replacing .Display with .Send has the same result.

The SentOnBehalfOfName property makes sense only in case of Exchange profiles/accounts. Moreover, you need to have the required permissions to send on behalf of another person. See Issue with SentOnBehalfOfName for a similar discussion.
In case if you have multiple accounts configured in the profile you can use the SendUsingAccount property which allows to an Account object that represents the account under which the MailItem is to be sent.
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
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub

Related

Redirect selected emails and make reply-to address same as original sender

I receive support requests in a shared exchange mailbox in Outlook 2013.
I forward the mails, which I am supposed to treat, to an external ticketing system. Before forwarding, I copy the original sender's address into the reply-to address.
The following macro would be perfect if:
the original sender's address would be copied into the reply-to address, and
if it could set the category of the original mail to "myname" and mark it as done.
Sub BatchRedirectEmails()
Dim objSelection As Outlook.Selection
Dim i As Long
Dim objMail As Outlook.MailItem
Dim objRedirectMail As Outlook.MailItem
'Get all selected emails
Set objSelection = Application.ActiveExplorer.Selection
If Not (objSelection Is Nothing) Then
For i = objSelection.Count To 1 Step -1
If TypeOf objSelection(i) Is MailItem Then
Set objMail = objSelection(i)
'Redirect each email
Set objRedirectMail = objMail.Forward
With objRedirectMail
'Add more recipients as per your needs
.Recipients.Add ("john#datanumen.com")
.Recipients.Add ("abby#datanumen.com")
.Recipients.Add ("coral#datanumen.com")
.Recipients.Add ("david#datanumen.com")
.Recipients.ResolveAll
.Subject = objMail.Subject
.HTMLBody = objMail.HTMLBody
.Send
End With
End If
Next
End If
End Sub

How to set where an email is sent from?

I have some email accounts configured. One of these emails (e.g. myemail#mail.com) gives access to some mailboxes (e.g. emaiolbox#mail.com).
If I choose "From" > "Other Email Address", then I can choose which account I want to use ("Send Using") and which email ("From").
I have a macro to reply to emails.
I'd like to set the "sent using" and "from" options.
Sub send_email()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate("C:\Utils\Outlook_Templates\macro.oft")
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
replyEmail.Subject = "RE: " + origEmail.Subject
replyEmail.To = origEmail.Sender
replyEmail.CC = origEmail.CC + ";" + replyEmail.CC
replyEmail.Display
End Sub
There are two possible options available in the Outlook object model:
The SendUsingAccount property allows setting an Account object that represents the account under which the MailItem is to be sent. The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called. This property returns Null (Nothing in Visual Basic) if the account specified for the MailItem no longer exists.
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
The SentOnBehalfOfName property allows setting a String indicating the display name for the intended sender of the mail message. Note, in that case you need to be sure that you have sufficient permissions to send emails on behalf of another person.

How to send mail from shared email account?

I am trying send several items at the same time.
I have two mail addresses in my Outlook. The first is a personal work mail (like j.doe#company.com) and second is a shared mail account (like support#company.com).
I tried:
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
I found this won't work with shared email accounts.
Most forums advise to use .SentOnBehalfOfName. When I try it on one email, the email has something like this in the Sender box j.doe#company.com - Sent on behalf of name: "support#company.com.
When I send mail from Outlook manually, it only shows the shared account on the received message. (It is not a big deal, but would be nicer if the code would work in the same style as manually.)
Also, when I put the code in a loop, half mails are sent with shared accounts and half with personal account.
And here is the code with .SentOnBehalfOfName which is going to be looped.
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
This code attempts to ensure the .SentOnBehalfOf is consistent.
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

Send from another email address in Outlook

My users have their personal mailbox as their primary account and an auto-mapped shared mailbox configured in their Outlook 2010 client. The shared mailbox is an Office 365 shared mailbox and thus cannot be logged into to set it as the primary account.
I am trying to start a new email from the shared account's address.
Below is the VBA code I have been trying to use. I have allowed Macros in Outlook's trust center settings.
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
Using the following code with the SentOnBehalfOfName property starts a new email from the shared mailbox's address. Thanks to Dmitry Streblechenko for pointing me in the right direction.
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
A delegate mailbox will not be in the Namespace.Accounts list.
Set the MailItem.SentOnBehalfOfName property instead.
What does not work in your code?
Use the smtpAddress property to select an account.
Example function:
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
The debug.print line will show you the accounts.
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
Not exactly the problem I had, but this question and the answers helped. My problem was to send from a particular mailbox while using many accounts. The one-liner answer was this:
OutMail.SendUsingAccount = OutApp.Session.Accounts.Item("address#domain.com")

How to refresh ActiveInspector?

I changed the sender data in the currently open mail.
This is well done by the following code:
Sub AktiveMailSetVonHotline()
Dim oMail As Outlook.MailItem
Set oMail = ActiveInspector.CurrentItem
oMail.SentOnBehalfOfName = "Hotline#mycompany.de"
End Sub`
I cannot see that the sender is set as desired. For this, I'd like to refresh the visible Mail (inspector window).
It looks like you are interested in the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent. For example:
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
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
To make sure the From label shows the right value, you need to set the PR_SENT_REPRESENTING_EMAIL_ADDRESS property (DASL name http://schemas.microsoft.com/mapi/proptag/0x0065001F) using MailItem.PropertyAccessor.SetProperty.