使用PowerShell
发送电子邮件所以下面的代码工作,如果我手动添加电子邮件地址$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()
}
非常令人费解的代码示例。这里的东西要简单得多:
Import-Module ActiveDirectory
$csv = Import-Csv C:\Users\user\Desktop\Scripts\User.csv
ForEach ($item in $csv)
{
$User = Get-ADUser $item.Username -Properties @('userprincipalname','displayname')
[email protected]{
ErrorAction='Stop'
To = $item.mgremail
From = '[email protected]'
SmtpServer = '[email protected]'
Subject = 'Test'
Body = @"
Hi,
Name: $($User.displayname)
User Logon: $($User.userprincipalname)
Regards,
TEST
"@
}
Try {
Send-MailMessage @Params
} Catch {
Write-Host ('Failed to send email. ' + $PSItem.Exception.Message) -ForegroundColor Red -BackgroundColor White
}
}
@ShivPatel确保在您打算指向的服务器上启用角色。这里是[帮助主题](https://social.technet.microsoft.com/Forums/exchange/en-US/9c99624f-4379-4f19-85a3-5e2c58/how-to-find-smtp-address-on-exchange -server?forum = exchangesvradminlegacy) – TheIncorrigible1
如果我没有权限在服务器上启用此角色,该怎么办? –
我写了一个脚本,而回过去曾通过一个1级的团队,将在Outlook中生成他们的电子邮件时,他们正在帮助最终用户。它没有发送电子邮件(出于工作流程的原因),但它可以很简单地发送。这应该至少给你一些东西,以自动的方式添加收件人到电子邮件。
$Outlook = New-Object -ComObject Outlook.Application
$MailItem = $Outlook.CreateItem(0)
$MailItem.GetInspector.Activate()|Out-Null
$Signature = $MailItem.HTMLBody
$CCAddr = $MailItem.Recipients.add($User.email)
$CCAddr.Type = 2
$CCAddr.Resolve()|Out-Null
$MailItem.Recipients|?{$_.Type -eq 1}|%{$_.Delete()}
$ToAddr = $MailItem.Recipients.add($Item.MgrEmail)
$ToAddr.Resolve()|Out-Null
$MailItem.Subject="Task description"
$MailItem.SentOnBehalfOfName='Team DL'
$MailItem.HTMLBody = $HTMLBody + $Signature
$MailItem.GetInspector.Activate()|Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
Remove-Variable Outlook,MailItem
我已经定义为包含在电子邮件的正文中的HTML之前在脚本$HTMLBody
一个字符串这里。这将打开一封新邮件,初始化它,将团队的发布列表添加到CC行,将用户添加到收件人行(已更新以反映您的$Item.MgrEmail
),设置电子邮件的发送者为谁,以便它来自团队的DL ,并保留用户的签名。它的工作非常好,除了GUI中的Send As选项没有准确地反映我设置发送电子邮件的值(它仍然正确地从DL发送,它只是在GUI中显示了它们的个人别名)。
你显然可以省略一些东西,但我想我会提供这个,因为我用它并知道它的工作原理。
试试这个一个简单的SMTP发送:
$smtpserver="smtp server"
$from = "email address"
$to = "email address"
$subject = "subject"
$body = "What ever you want...."
$secpasswd = ConvertTo-SecureString “password” -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd)
Send-MailMessage -smtpServer $smtpserver -Credential $creds -Usessl true -from $from -to $to -subject $subject -attachment $body
这是一个更容易,只需使用'发送-MailMessage'。 –
您应该测试空值。 '如果(-not $ mgrmail){}' – TheIncorrigible1