【问题标题】:PowerShell Send EmailPowerShell 发送电子邮件
【发布时间】:2015-12-24 03:18:24
【问题描述】:

See this question's first two answers(都试过了,都报下面的错误):

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

$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 需要身份验证。 了解更多信息,请访问

在第一个脚本中,明确指定了端口,而它没有使用 PS 的内置函数(虽然我过去没有遇到过这个函数的问题)。

谢谢!

【问题讨论】:

  • 我认为this 可能会有所帮助。

标签: email powershell


【解决方案1】:

我们通过 Office365 使用 Powershell 发送邮件。这就是我们使用的(完美运行)。请记住,与您进行身份验证的用户必须具有发件人地址的“发送为”权限:

$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

【讨论】:

    【解决方案2】:

    尝试使用此功能通过 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
    

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 2021-05-09
      • 2016-07-21
      • 1970-01-01
      相关资源
      最近更新 更多