C#脚本如下:
if (-not((Get-PSSnapin) -match "Microsoft.Exchange.Management.PowerShell.E2010")){ Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 }
$strScriptName = $MyInvocation.MyCommand.Name
# 以下定义一些变量以方便测试
$strMsgFrom = "<
test@abc.com>"
$strMsgTitle = "Welcome to Edudcational Alliance Messaging System"
$strMsgTitle2="Second Email"
# 以上字段定义了发件人以及标题,接下来我们为发SMTP邮件建立一个新的对象
$SMTPClient = New-Object Net.Mail.SmtpClient("10.110.0.86")
# 括号内的是集线器传输服务器的IP地址
# 接下来我们截取需要接受这些邮件的邮箱地址列表
$MBXArray = @(get-user –organizationalUnit KU-Test | where-object {$_.RecipientType –eq “User” } | Enable-Mailbox –Database KU-Test |Get-Mailbox) #-Database KU-Test -ResultSize Unlimited)
Start-Sleep -Second 10
# 收件人从$MBXArray中获取
ForEach ($mailbox in $MBXArray ) {
$strMsgTo = $mailbox.PrimarySMTPAddress.tostring()
$strMsgBody = "Hello, "+$mailbox.DisplayName+", and welcome to the Educational Alliance Messaging System! Please keep this email for future use. It contains vital information.
--------------------------------------
Username and password
--------------------------------------
Your network username is '"+$mailbox.SamAccountName+"'. Use your username and password to login to the network. Your password should NEVER be shared with anyone except the I.T. department, and only then when requested. Please do not write it down on anything that can be seen by your coworkers. You will be prompted to change it regularly.
--------------------------------------
Email
--------------------------------------
Your email address is '"+$mailbox.PrimarySMTPAddress+"'.
To access your email, calendar, contacts, and tasks from outside of the building, such as from home, you can do so from any Internet connected computer. Simply open Internet Explorer and go to the Outlook Web Access (OWA) page a
Thank you, and, again, welcome to HEC Education Alliance Messaging System!
The Information Technology Department"
##### 上面是第一封邮件的正文,下面写第二封
$strMsgBody2 = "Hello, "+$mailbox.DisplayName+", and welcome to the Educational Alliance Messaging System! Please keep this email for future use. It contains vital information.
This is Second Email
Thank you, and, again, welcome to HEC Education Alliance Messaging System!
The Information Technology Department"
# 下面是发送邮件的代码
$SMTPClient.Send($strMsgFrom,$strMsgTo,$strMsgTitle,$strMsgBody)
$SMTPClient.Send($strMsgFrom,$strMsgTo,$strMsgTitle2,$strMsgBody2)
}