How to set where an email is sent from? - vba

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.

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 send mail from a specific account?

I am using Office 365 (Outlook Version 1909 Build 12026.20264).
I configured two email accounts (a#a.com and b#b.com) and one shared mailbox (shared#b.com).
With my b#b.com account I have permission to send email from shared#b.com.
I can manually send email from all accounts (a#a.com, b#b.com, shared#b.com).
I have VBA code to send email from shared#b.com using the SentOnBehalfOfName property.
Sub TEST()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate("C:\...\Template.oft")
replyEmail.SentOnBehalfOfName = "shared#b.com"
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
replyEmail.Subject = "RE:" + origEmail.Subject
replyEmail.Display
End Sub
Usually this sends emails correctly. I can see they go to the sent folder in the b#b.com account.
Sometimes it tries to send emails using the a#a.com account (which has no permission to send emails from shared#b.com). I receive an error email in a#a.com and the email is not sent.
How can I change my code so that everytime I am sending email from shared#b.com it will use my b#b.com account?
Note: I set my b#b.com account as my default account.
You need to set the SendUsingAccount property to the Account object corresponding to the b#b.com mailbox.
Sub TEST()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate("C:\...\Template.oft")
for each acc in Application.Session.Accounts
if acc.DisplayName = "b#b.com" Then
replyEmail.SendUsingAccount = acc
Exit for
End If
next
replyEmail.SentOnBehalfOfName = "shared#b.com"
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
replyEmail.Subject = "RE:" + origEmail.Subject
replyEmail.Display
End Sub

Outlook vba - How can i add suffix on mail subject before forward, reply or replay to all?

I need to forward or reply to a mail, then add suffix on subject, and send it.
thanks in advance
You could reply or replyAll an email and add suffix to subject using the below code:
Option Explicit
Sub ReplyMSG()
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply
Dim olRecip As Recipient ' Add Recipient
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
olReplay.Subject = = olItem.Subject & “suffix”
Set olRecip = olReply.Recipients.Add("Email Address Here") ' Recipient Address
olRecip.Type = olCC
olReply.HTMLBody = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
olReply.Display
'olReply.Send
Next olItem
End Sub
Reference from:
Outlook Reply or ReplyAll to an Email
If you want to forward an email and add suffix to subject, please refer to the below code:
Sub ForwardEmail(item As Outlook.MailItem)
Dim oMail As MailItem
On Error GoTo Release
If item.Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject & “suffix”
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"
oMail.Save
oMail.Send
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub
Reference from:
Forward Email with its attachment in Outlook 2010
Properly going to be hard, because depending on your hierarchy location you would only have a set number of rights and i do not think VB Macro would be allowed i know in many organizations here in Denmark VB Macros is a no no.
But you can create a Macro in the background where you grep Current mail and then alter the subject and then set it to when you press CTRL + R as you normally do when you reply but it might conflict so be careful because you might not want it on all emails. that you reply to.

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.