1

I use powershell to send emails from Outlook (complete automatic process). This work perfectly for the default mailbox. I have a lot of restrictions in the development network and can't use any software other than Outlook and powershell.

My question is: Is there a method to send email from a different account in outlook, using powershell (in my Outlook I have three accounts A, B and C), even if i have one of the as predetermined.

The code that I use is this.

$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = "SUBJECT"
$mail.body = "BODY"
$mail.To = "[email protected];"
$mail.Send()

Is there a property I can set to make the email origin to be B or C, and not the predetermined A.

8
  • clarified question, and added the appropriate tags Commented Apr 1, 2015 at 21:21
  • You could probably set the "Reply To" delivery options. But unless Outlook has been configured with delegation permissions for the other account, any other option won't be available. Commented Apr 1, 2015 at 21:31
  • I'd really, really recommend looking at EWS, which is just a DLL that must ship with your powershell script. Automating Outlook is very limited from an automated script, due to all the malicious software that was written in the early 2000's that made use if the feature to propagate spam, virusses and steal whole address books. Commented Apr 1, 2015 at 21:32
  • See: msdn.microsoft.com/en-us/library/… Commented Apr 1, 2015 at 21:36
  • And: msdn.microsoft.com/en-us/library/… Commented Apr 1, 2015 at 21:36

1 Answer 1

1

You need to set the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent. For example:

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 ("[email protected]") 
     oMail.Recipients.ResolveAll 
     oMail.SendUsingAccount = oAccount 
     oMail.Send 
   End If 
 Next 
End Sub 

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.