Send from another email address in Outlook - vba

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")

Related

.SentOnBehalfOf and .SendUsingAccount on forward

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

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.

Trigger when composing e-mail

This code is executed every time I select a message. I want this code to be executed only when I am composing new mail/replying/forwarding.
Private Sub Application_ItemLoad(ByVal Item As Object)
Dim oAccount As Outlook.Explorer
If TypeName(Item) = "MailItem" Then
MsgBox ("this is mail")
Set oAccount = Application.ActiveExplorer
MsgBox (oAccount.CurrentFolder.FolderPath)
End If
End Sub
What I am trying to achieve: When composing mail from specific account (account#mail.com) add recipient in CC field.
I am a noob in programming.
You can test the MailItem.Sent property to determine whether composing mail or reading received mail.
Private Sub Application_ItemLoad(ByVal Item As Object)
Dim oAccount As Outlook.Explorer
If TypeName(Item) = "MailItem" Then
If item.Sent Then
MsgBox "Not for processing"
Else
MsgBox "Processing this mail"
Set oAccount = Application.ActiveExplorer
MsgBox (oAccount.CurrentFolder.FolderPath)
End If
End If
End Sub

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.

Macro to delete an email

I have created a macro that forwards an email on to a recipient once a button is clicked. However, I want the macro to also delete the email (sending it to the recycle bin).
Here is the current code. This currently works and forwards the email.
Sub forwardEmail()
Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.forward
oMail.Recipients.Add "Recipients email goes here"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If
End Sub
I thought by adding oMailItem.Delete to the code would work but it does not.
It wasn't clear to me which email you wanted deleted, the original email or the forwarded email from Sent items - so these mods provide both options.
Sub forwardEmail()
Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.Forward
oMail.Recipients.Add "spam_me"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
'delete forwarded email from sent items
oMail.DeleteAfterSubmit = True
oMail.Send
'delete original email from inbox
oOldMail.Delete
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If
End Sub