PowerShell Outlook Send Email

I am developing a script that involves creating an email contact and forwarding mail to that contact. The last part of the script is to automatically send a test email to the address to make sure the forwarding is working.

So I am using the following code:

[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Core")
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Outlook")

$olApp = New-Object Microsoft.Office.Interop.Outlook.ApplicationClass
$msg = $olApp.CreateItem(0)
$msg.Recipients.Add("me@example.com")  
$msg.Subject = "test"  
$msg.Body = "test"  
$msg.Send()

      

I am getting an error on line 6: "You cannot call a method on a null-valued expression."

I run the code at home, it works fine. Difference: In a domain at work, using Exchange Server at work, using a domain account at work.

I am using the same version of Powershell and Outlook on both machines. It is preferable to use Outlook to send the message because I already have Outlook open and that way the message will appear in my Sent Items folder.

0


source to share


2 answers


I had similar issues where some of the COM objects didn't seem to fill up completely (looks like something to do with the corporate Outlook installation - antivirus measures perhaps?). However, if you comment out the line $msg.Recipients.Add("me@example.com")

and add $msg.Display()

, you can add recipients and send from there. Not a very good solution.

For my question, I pointed to Exchange Web Service (EWS), so I downloaded EWS API 2.2 and used the following:

Add-Type -LiteralPath "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2010_SP1
$service.AutodiscoverUrl(β€˜me@mydomain.com’, {$true})

$msg = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$msg.Subject = "Sent from Powershell EWS"
$msg.Body = "<html><body><h1>TESTING</h1><p>This is a test message sent from Powershell!</p></body></html>"
$msg.ToRecipients.Add("me@mydomain.com")
$msg.SendAndSaveCopy()

      

It seemed to work pretty well and saved the message in my folder.



If you don't want the item to be saved in the Sent Items folder (for example, adding yourself as a recipient), it might be easier to just use Send-MailMessage with "NT AUTHORITY \ ANONYMOUS LOGON":

$emptySecStr = New-Object System.Security.SecureString
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $emptySecStr
Send-MailMessage -SmtpServer $smtpServerName -Credential $creds -From $sendingAddr -to $sendToArray -Cc $sendCCArray -BodyAsHtml -body $htmlBody -Subject $subject

      

I got this technique from a great discussion here .

+1


source


If you've changed your mind about using Outlook, take a look at the PowerShell Community Extensions (free) on CodePlex. They offer a cmdlet to send SMTP email, which is enough to validate the newly generated address. Not sure if there is a value when doing a test in your dispatched items? Especially if you're doing it in bulk - it would be much faster to use SMTP directly than to use Outlook.



0


source







All Articles