vbaemailoutlook

Sending email from other account


I send emails through Outlook using Excel VBA. I am using my work computer with my work email as the main account, but want to send from another account that is logged in.

I have not managed to integrate any of the code found online.

The below code is without my attempt of fixing it.

Sub Test1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim strbody As String
    For Each cell In Range("D2:D2")
        strbody = strbody & cell.Value & vbNewLine
    Next

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "C").Value) = "yes" Then

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "A personal message from the founder"
                .Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
                .Send
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Solution

  • There are two possible ways in Outlook:

    1. If another account is configured in Outlook you need to use the MailItem.SendUsingAccount property which returns or sets 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 
     Set oMail.SendUsingAccount = oAccount 
         oMail.Send 
     End If 
     
     Next  
    End Sub
    
    1. If you have got permissions set by the Exchange admin to send on behalf of another person you need to use the MailItem.SentOnBehalfOfName property which returns a string indicating the display name for the intended sender of the mail message.