How to send mail from a specific account? - vba

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

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.

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.

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 an Email using PowerPoint VBA

I want to send an email (with subject, body...) to another email address.
I tried the following code, but it didn't work:
Private Sub CommandButton1_Click()
Dim ret As Boolean
Dim strAddress As String
Dim strMessage As String
strAddress = "examplemail#gmail.com"
ret = SendEMail(strAddress, (Label1.Caption), strMessage)
Label1.Caption = ret
If Label1.Caption = "True" Then
MsgBox "Mail sent!"
ElseIf Label1.Caption = "False" Then
MsgBox "Mail not sent!"
End If
End Sub
Public Function SendEMail(strRecipient As String, strSubject As String, strBody As String) As Boolean
Dim oApp As Object
Dim oMail As Object
Err.Clear
On Error Resume Next
Set oApp = GetObject(Class:="Outlook.Application")
If Err <> 0 Then Set oApp = CreateObject("Outlook.Application")
Err.Clear
Set oMail = oApp.CreateItem(0)
With oMail
.Subject = strSubject
.To = strRecipient
'copy to self
.CC = "youraddy#you.com"
.BodyFormat = 1
.Body = strBody
.Send
End With
'cleanup
Set oMail = Nothing
Set oApp = Nothing
'All OK?
If Err = 0 Then SendEMail = True Else SendEMail = False
End Function
The code was originally taken from here.
If it is possible, I want a code that is compatible with most PCs.
Using Microsoft Outlook
For sending an e-mail you need to have an e-mail account in Microsoft Outlook configured, because your code is using Outlook to send the e-mail.
Set oApp = GetObject(Class:="Outlook.Application")
Alternative 1: SMTP
Alternatively you could set up an SMTP connection in VBA to send the e-mails over an external mail server using CDO. More information on the usage of CDO in VBA can be found here (even if they write the code is for Excel, you can use it for PowerPoint as well) and here as well.
The drawback of this approach is, that the SMTP login credentials are visible in the VBA code. This could be a security issue if you plan to share this presentation with other people.
Alternative 2: Mailto-Link
A third way would be to offer the user a link to click on in order to send the e-mail: mailto:recipient#example.com?subject=xxx
A description for this approach can be found here (scroll down to the third option).