views:

891

answers:

1

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.

A: 

If you change your mind about using Outlook, look at the PowerShell Community Extensions (free) on CodePlex. They offer a cmdlet to send SMTP e-mail, which would suffice to test the newly-created address. Not sure if there's value in having the test in your Sent Items? Especially if you're doing these in bulk - it'd be much faster to use SMTP directly than to use Outlook.

Don Jones
Stuff I write is implicitly authorized to be on the network. Stuff that other people write needs to go through a big approval process. This isn't for bulk necessarily, but rather to turn what used to be a 5 minute job into a 30 second task. Also, I don't know the SMTP server.
Orihara