Reputation: 117
I am trying to write a simple powershell script to send mails and the code I have written is:
$recipients = "'[email protected]', '[email protected]'"
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = $recipients
$Mail.Subject = "Action"
$Mail.Body ="Pay rise please"
$Mail.Send()
this is working with one recipient, but once I want to add multiple contacts and run the script, it says
Outlook does not recognize one or more names.
At line:10 char:1
+ $Mail.Send()
+ ~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
I have tried a number of ways to define recipient list by using:
"'[email protected]', '[email protected]'"
"[email protected]", "[email protected]"
"[email protected], [email protected]"
"<[email protected]>, <[email protected]>"
nothing worked and returns the same error every time.
Thanks in Advance!
Upvotes: 0
Views: 1819
Reputation: 36332
You can use the Recipients
property's Add()
method to add multiple recipients to an email if you're using Outlook's ComObject, such as:
'[email protected]','[email protected]'|ForEach{$Mail.Recipients.Add($_)}
Doing this will return the recipient object, which you can then manipulate, such as, if you wanted to put them on the CC line instead of the To line you could do:
$Recipients = '[email protected]','[email protected]'|ForEach{$Mail.Recipients.Add($_)}
$Recipients | ForEach{$_.Type = 2}
Or resolve the address against the user's address book using the Resolve()
method.
Upvotes: 2