vbaoutlook

How to set MailItem.SendUsingAccount when sending an email?


I need to automatically sign in to Outlook and send an email.

The documentation says the SendUsingAccount property is available to set an Account from which a mail is sent. It seems to not explain how.


Solution

  • The other account should be configured in Outlook, for example, here is a VBA sample code:

    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