PowerShell发送邮件



看这个问题的前两个答案(两个都试过了,都产生了下面的错误):

代码(更改了必要的部分):

$EmailFrom = "notifications@somedomain.com"
$EmailTo = "me@earth.com" 
$Subject = "Notification from XYZ" 
$Body = "this is a notification from XYZ Notifications.." 
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

其他方式(更改必要的部分):

$credentials = new-object Management.Automation.PSCredential “mailserver@yourcompany.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl

我得到这个错误:

SMTP服务器需要安全连接,或者客户端没有安全连接身份验证。服务器响应为:5.5.1 Authentication Required。在

了解更多信息

在第一个脚本中,端口被显式指定,而它没有使用PS的内置函数(尽管我过去没有遇到过这个函数的问题)。

谢谢!

我们通过Office365用Powershell发送邮件。这就是我们用的(效果很好)。请记住,您进行身份验证的用户必须具有from-address:

的"send as"权限。
$PSEmailServer = "smtp.office365.com"
$credentials = new-object Management.Automation.PSCredential “UserLogonName@domain.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)
$enc  = New-Object System.Text.utf8encoding
$from = "FromAddress"
$to = "ToAddress","ToAdress2"
$body = "Test"
$subject = "Test"
Send-MailMessage -port 587 -From $from -BodyAsHtml -Encoding $enc -To $to -Subject $subject -Body $body -UseSsl -Credential $credentials

试试这个函数通过Gmail发送电子邮件:

Function  batchsend-mailmessage ($to, $subject, $body, $attachments)
{
  $smtpServer = "smtp.gmail.com"
  $smtpServerport = "587"
  $emailSmtpUser = "TheBatch@gmail.com"
  $emailSmtpPass = "PasswordOfTheBatch"
  $emailMessage = New-Object System.Net.Mail.MailMessage
  $emailMessage.From = $emailSmtpUser
  foreach ($addr in $to)
  {
    $emailMessage.To.Add($addr)
  }
  foreach ($attachment in $attachments)
  {
    $emailMessage.Attachments.Add($attachment)
  }
  $emailMessage.Subject = $subject
  $emailMessage.IsBodyHtml = $true
  $emailMessage.Body = $body
  $smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport)
  $smtpClient.EnableSsl = $true
  $smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass);
  $SMTPClient.Send( $emailMessage )
}

像这样:

batchsend-mailmessage -to $DesAdressesDest -subject $Subject -body $body -attachments $xlsFile

相关内容

  • 没有找到相关文章

最新更新