-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750
对于管理员来说需要经常测试线上系统的服务运行状态,powershell的send-mailmessage命令是运用比较频繁的,命令如下:
1、Send-MailMessage -From [email protected] -To "[email protected]" -Subject "test" -Credential "[email protected]" -SmtpServer mail.yuntcloud.com -Port 587
2、发送中文字符邮件时候会出现字符乱码,我们需要加上参数-Encoding ([System.Text.Encoding]::UTF8)
Send-MailMessage -From [email protected] -To "[email protected]" -Subject "test" -Credential "[email protected]" -SmtpServer mail.yuntcloud.com -Port 587 -Encoding ([System.Text.Encoding]::UTF8)
3、我们可以把这个密码转换成密文密码保存下来,供powershell直接调用,命令如下:
$UserName = "[email protected]" #定义管理员账户名称
$Password = ConvertTo-SecureString Aa543cd -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
Send-MailMessage -From [email protected] -To "1854[email protected]" -Subject "test" -Credential $cred -SmtpServer mail.yuntcloud.com -Port 587 -Encoding ([System.Text.Encoding]::UTF8)
4、发送一封带附件的说明邮件
$nMsg = "
<br>您好!</br>
<br>附件是您企业的用户邮箱状态报告:</br>
<b><br>这是一封系统自动发出的邮件,请不要直接回复或转发这封邮件。</br></b>
<br>有任何疑问请邮件联系<a href=mailto:[email protected]>专业技术支持</a></br>
"
$date = get-date #获取当前日期
$nSmtpserver = "mail.yuntcloud.com"
$nFrom = "[email protected]"
$nTo = "[email protected]"
$nSubject = "企业邮箱用户最新使用情况, $date"
$UserName = "[email protected]" #定义管理员账户名称
$Password = ConvertTo-SecureString Aa543cd -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
send-mailmessage -BodyAsHtml -subject $nSubject -Smtpserver $nSmtpserver -From $nFrom -To $nTo -body $nMsg -Attachments ("c:\企业用户邮箱最新使用情况.csv","c:\企业存档邮箱最新使用情况.csv") -Credential $cred -SmtpServer mail.yuntcloud.com -Port 587 -Encoding ([System.Text.Encoding]::UTF8)
write-host "完成,企业邮箱用户及存档邮箱最新使用情况已发送给 $nTo"