vbaoutlookoffice-automation

how to set an Account object by MailItem.SendUsingAccount when sending an email?


I need to build a program that automatically sign in to Outlook and send an email using the account.

The doccument says SendUsingAccount property is available to set an Account object from which a mail is sent, yet it seems not explain how.

Please help me to find how to do it. Thank you!


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