I'm developing a script that involves creating an email contact, and forwarding mail to that contact. Last part of the script is to automatically send a test email to the address to make sure the forwarding works.
So I use the following code:
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Core")
$olApp = new-object Microsoft.Office.Interop.Outlook.ApplicationClass
$msg = $olApp.CreateItem(0)
$msg.Recipients.Add("[email protected]")
$msg.Subject = "test"
$msg.Body = "test"
$msg.Send()
I get an error at line 6: "You cannot call a method on a null-valued expression."
I run the code at home, it works just fine. Difference: on a domain at work, using exchange server at work, using domain account at work.
I am using the same version of powershell and outlook on both machines. It's preferable to use Outlook to send the message because I already have Outlook open and that way the message will show up in my Sent Items folder.