Send email from another address using VBA

Last reviewed on February 13, 2014

I need to send message from the certain E-mail address (E-mail Distribution Group) with VBA code. I have permission "Send as" and I can do it by hands, changing field "From".

Use SentOnBehalfOfName, and as long as your Exchange account has SendAs permission for the shared mailbox or distribution group, it will be sent from the shared account or group, not sent on behalf of.

Send as using VBA

If you want to send from another account in your Account list, you'll use SendUsingAccount and select the account by index or by name. See Macros to send message using the default account or a specific account for code samples.

SendAs an address code Sample

Note: this macro is used with Exchange account to send from addresses you have SendAs permissions.


Sub CustomMailMessage()
 
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Recipient
Dim Recipients As Recipients
 
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(olMailItem)
 
Set Recipients = objOutlookMsg.Recipients
Set objOutlookRecip = Recipients.Add("alias@domain.com")
objOutlookRecip.Type = 1
  
objOutlookMsg.SentOnBehalfOfName = "sales@domain.com"
 
objOutlookMsg.Subject = "Testing this macro"
 
objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf

'Resolve each Recipient's name.
For Each objOutlookRecip In objOutlookMsg.Recipients
objOutlookRecip.Resolve
Next
 
'objOutlookMsg.Send
objOutlookMsg.Display
 
Set OutApp = Nothing
 
End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

13 responses to “Send email from another address using VBA”

  1. Chris

    looks like the

    objOutlookmsg.MailItem.Sender = "sendas_address@domain.com"

    is all a bit wrong, and since this is the crux of this post, I think its a biggie.

    Since the objOutlookmsg is already set as a MailItem object, there is no MailItem property or method - so objOutlookmsg.MailItem wont work.

    If you ignore the MailItem as a typo, then

    objOutlookmsg.Sender = "sendas_address@domain.com"

    still wont work.

    The line results in a type mismatch. The Sender property is of type AddressEntry, but you are simply setting this as a string.

    I actually want to achive this, and although I can see whats wrong, I don't know how to get an AddressEntry object or set the Sender property.

    Help please required

  2. Chris

    Yes. You simply have a type mismatch here.

    What can work is:

    objOutlookmsg.SentOnBehalfOfName = "foo@bar.com"

    But this only works on exchange I believe. the .Sender is user when you have multiple accounts setup in Outlook. I have seen code the enumerates the accounts and gets the AddressEntry, but maybe you simply want to program a specific AddressEntry - I'm not sure how to set this.

  3. Ebony Rivera

    Could you please help me. I send a repetitive email at least 20 times per day. I need a VBA code that will simply generate the message with it's contents. Does that make any since? I've been at this with no luck for 2 days. Please help me.......

  4. Andry

    I'd be glad to assist. Please copy and paste the text you want o send and I'll embed, or just use the example below:

    ================================================================
    Sub SendEmail()
    Dim objEmail As Outlook.MailItem
    Set objMailApp = CreateObject("Outlook.Application")
    Set objEmail = objMailApp.CreateItem(0)
    With objEmail
    .Subject = "Repetitive Email SUBJECT"
    .To = "me@mydomain.com"
    .CC = "you1@mydomain.com; you2@mydomain.com"
    .BCC = "you3@mydomain.com"
    .HTMLBody = "Hey guys,This email is now automatedThx,Ebony Rivera"
    .BodyFormat = olFormatHTML
    .Attachments.Add ("C:\My Documents\Picture.jpg") 'if you want to attach a file
    .DeleteAfterSubmit = True 'if you want to delete the copy the system leaves in your SENT TO folder use =True, or if not, use =False
    .ExpiryTime = Now() + 1 'expires in 1 day - delete line if you don't want the email to expire; change "1" to "X" if you want "X" days expiration
    .Importance = olImportanceHigh ' if you want to send as HIGH IMPORTANCE
    .Send
    End With
    Set objMailApp = Nothing
    Set objEmail = Nothing
    End Sub
    ================================================================

  5. ali00000a

    This doesnt work on outlook 2010, you need to use the SentOnBehalfOfName attribute instead of Send.

  6. Hakan

    HOW CAN I SELECT LAST RECEIVED EMAIL AND SAVE IT TO LOCAL FOLDER? :)
    YOU MAY FIND MY CODE BELOW. THANKS..

    Sub SaveSelectedMailAsTxtFile(item As Outlook.MailItem)

    Const OLTXT = 0
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    Dim oMail As Outlook.MailItem
    Dim obj As Object
    Dim sPath As String
    Dim dtDate As Date
    Dim sName As String

    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection

    For Each obj In Selection
    Set oMail = obj
    ' sName = oMail.Subject
    'ReplaceCharsForFileName sName, "_"

    'dtDate = oMail.ReceivedTime
    'sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt"
    sName = "Fuel" & Date & ".txt"
    oMail.SaveAs "C:\Users\hgokceoglu\Desktop\test\" & sName, OLTXT

    Next
    MsgBox "rule çalıştı"

    End Sub

  7. mrsklb

    Hi Diane,
    This looks perfect.
    We have a helpdesk and the 'user' is the person that sends in the email, I want to be able to forward email I receive directly to the helpdesk but define the 'sender' as the customer so the ticket is created in their email account, not mine.

    Is this possible using this code teamed with a 'forward email' code?
    I've seen one elsewhere but it will always send as me, not as the originiator which is not what I need.

    When the email hits the helpdesk the helpdesk auto responds with ticketID etc, which the customer needs for follow up.

  8. Karen

    Thanks Diane, sorry, I registered under the forum as MrsAdmin and didn't update here that we've been working on it. I have to update that thread too tomorrow (my 12yr old has been sick & I haven't had a chance to catch up on my topics). Thank you for getting back to me :)

  9. Darren S

    SentOnBehalfOfName attribute works brill !

Leave a Reply

If the Post Coment button disappears, press your Tab key.