PowerShell发送邮件(587)

    #定义邮件服务器
    $smtpServer = "mail.xx.com"
    $smtpUser = "sender"
    $smtpPassword = "password"
    $mail = New-Object System.Net.Mail.MailMessage

    #定义发件人邮箱地址、收件人邮箱地址
    $MailAddress="sender@xx.com"
    $MailtoAddress="l1@xx.com"

    $mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
    $mail.To.Add($MailtoAddress)

    #定义邮件标题、优先级和正文
    $mail.Subject = "Test587";
    $mail.Priority  = "High"
    $mail.Body = "Test Mail"
    $smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer,587 #使用587端口
    $smtp.Enablessl = $true  #使用TLS加密
    $smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
    $smtp.Send($mail)

发送html格式邮件

#密码过期提醒发送邮件  -2017-07-18

#定义邮件服务器
$smtpServer = "mail.x.com"
$smtpUser = "user@x.com"
$smtpPassword = "password"
$mail = New-Object System.Net.Mail.MailMessage

#定义发件人邮箱地址、收件人邮箱地址
$MailAddress="user@x.com"
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.IsBodyHtml = $True #定义为HTML格式邮件

#定义邮件标题、优先级和正文
$mail.Subject = "提醒:";
$mail.Priority  = "High"
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer,587 #使用587端口
$smtp.Enablessl = $true  #使用TLS加密
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword

$MailtoAddress="l@x.com"
$mail.To.Add($MailtoAddress)
$mail.Body = "<html><body><span style='font-size:10.5pt;font-family:宋体'>
您好,
<br><br>  &nbsp;&nbsp;&nbsp;&nbsp;您的帐号 密码将会于3天后过期。请尽快修改!
<br><br>    &nbsp;&nbsp;&nbsp;&nbsp;修改方法:。
<br><br><br> <img  width=146 height=52 alt="""" src=""http://img.x.com/logo.png"" />
<br>-----------------------------------------
<br>这封邮件由管理程序自动生成,请勿直接回复!
</span></body></html>"
$smtp.Send($mail)

$smtp.UseDefaultCredentials = $false 要放在SmtpClient.Credentials之前

 If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server. If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously.

原文地址:https://www.cnblogs.com/dreamer-fish/p/6425338.html