2

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!

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
tam tem
  • 23
  • 4

2 Answers2

1
Option Explicit

Sub designateSendUsingAccount()

Dim sendingAccount As String
Dim outMail As MailItem
Dim i As Long
Dim foundFlag As Boolean

sendingAccount = "someone@somewhere.com"

Set outMail = CreateItem(olMailItem)

For i = 1 To Session.Accounts.count
    If LCase(Session.Accounts.Item(i)) = LCase(sendingAccount) Then
        outMail.SendUsingAccount = Session.Accounts.Item(i)
        foundFlag = True
        Exit For
    End If
Next

If foundFlag = True Then
    outMail.Display
Else
    MsgBox sendingAccount & " account not found."
End If

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
0

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
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45