- 编写一个脚本来发送邮件给新建的用户或新启用邮箱的用户,脚本取名为send-mail.ps1,将脚本放到Exchange服务器的C:\temp\路径下,脚本可以接受mailboxname的参数;
Send-mail.ps1 –mailboxname <邮箱名>
param
(
[string]$mailboxname
)
function sendmail($mailaddr){
$msg=New-Object System.Net.Mail.MailMessage
$msg.To.Add($mailaddr)#收件人
$msg.From = New-Object System.Net.Mail.MailAddress("[email protected]", "邮箱助手",[system.Text.Encoding]::GetEncoding("GB2312")) #发件人
$msg.Subject = "Welcome to canway"#邮件主题
$msg.SubjectEncoding = [system.Text.Encoding]::GetEncoding("GB2312")
$msg.Body ="你好,欢迎使用canway邮箱"#邮件正文
#$Attachments=New-Object System.Net.Mail.Attachment("D:\Documents\xxxx.zip")#创建附件
#$msg.Attachments.add($Attachments) #添加附件
$msg.BodyEncoding = [system.Text.Encoding]::GetEncoding("GB2312")
$msg.IsBodyHtml = $false#发送html格式邮件
#$msg.Priority = [System.Net.Mail.MailPriority]::High#邮件等级
$client = New-Object System.Net.Mail.SmtpClient("192.168.0.3")#一般是HUB服务器或者是内部SMTP gateway
$client.UseDefaultCredentials = $false
$client.Credentials=New-Object System.Net.NetworkCredential("[email protected]", "1qaz@WSX")#服务器的访问凭据
try {$client.Send($msg)}
catch [Exception]{$($_.Exception.Message)
$mailaddr
} }
$mailaddr = $mailboxname
sendmail $mailaddr
- 在Exchange的安装路径下,如在默认安装路径下的C:\Program Files\Microsoft\Exchange Server\V14\Bin\CmdletExtensionAgents文件夹中,创建一个XML文件ScriptingAgentConfig.xml,内容如下:
if($succeeded) {
start-sleep -s 10
Set-ADServerSettings -ViewEntireForest $true
$Alias = $provisioningHandler.UserSpecifiedParameters["Alias"]
$newmailbox=$Alias +"@"+"canway.com"
C:\temp\send-mail.ps1 -mailboxname $newmailbox
Set-CASMailbox $newmailbox -IMAPEnabled $false -POPEnabled $false
}
- 关闭所有服务器上目前打开的EMS和EMC,然后将ScriptingAgentConfig.xml文件拷贝到到环境中的每一台Exchange 服务器的C:\Program Files\Microsoft\Exchange Server\V14\Bin\CmdletExtensionAgents路径下;
- 在每台Exchange服务器的EMS上运行如下命令来开启CmdletExtensionAgent这个功能;
Enable-CmdletExtensionAgent "Scripting Agent"
- 新建一个邮箱用户的测试,如下测试成功。
New-Mailbox -Name 'user11' -Alias 'user11' -OrganizationalUnit 'canway.com/Test' -UserPrincipalName '[email protected]' -SamAccountName 'user11' -FirstName 'user11' -Initials '' -LastName '' -Password 'System.Security.SecureString' -ResetPasswordOnNextLogon $false -Database 'MailboxDatabase01'
- 新启用邮箱用户的测试
Enable-Mailbox -Identity 'canway.com/Test/user10' -Alias 'user10' -Database 'MailboxDatabase01'