发送电子邮件所以下面的代码工作,如果我手动添加电子邮件地址$Mail.To = ("[email protected]")
但一旦我禁用,然后从Excel列(mgremail)把它给了我这个错误使用PowerShell
There must be at least one name or contact group in the To, Cc, or Bcc box.At C:\Users\pshivam\Desktop\Scripts\test.ps1:60 char:5
+ $Mail.Send()
+ ~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我的代码:
Import-Module ac*
$csv = Import-Csv C:\Users\pshivam\Desktop\Scripts\User.csv
$password = ConvertTo-SecureString -String “Newuser1” -AsPlainText -Force
foreach($item in $csv){
$mgrmail = $item.mgremail
$sam =$item.Username
$displayname = (Get-ADUser $sam -Properties displayname).displayname
##name
$nameTitle = "Name: "
$Name = (Get-ADUser $sam -Properties cn).name
#upn
$upnTitle = "User Logon: "
$UPN = (Get-ADUser $sam -Properties userprincipalname).userprincipalname
$ol = New-Object -comObject Outlook.Application
$mail = $ol.createItem(0)
$Mail.To=($mgrmail)
#$Mail.To=("[email protected]")
$Mail.Subject="TEST"
$Mail.Body = "Hi,
"+ $nameTitle, $displayname, "
" + $upnTitle, $upn + "
TEST
"
$Mail.Send()
}
这是一个更容易,只需使用'发送-MailMessage'。 –
您应该测试空值。 '如果(-not $ mgrmail){}' – TheIncorrigible1