简体   繁体   中英

PowerShell Outlook send email

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")
[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 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.

I was having similar issues where some of the COM objects do not seem to populate fully (seems to have something to do with the corporate Outlook setup - perhaps anti-virus measures?). However, if you comment out the $msg.Recipients.Add("me@example.com") line and added $msg.Display() you can add the recipients and send from there. Not a very good solution.

For my question I was pointed towards Exchange Web Service (EWS) so I downloaded the 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()

This seemed to work pretty well and saved the message to my sent folder.

If you do not need the item to be saved in your sent-items folder (for example adding yourself as a recipient) it is probably easier to just use Send-MailMessage using "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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM