PowerShell发送电子邮件 [英] PowerShell Send Email

查看:138
本文介绍了PowerShell发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到这个问题的前两个答案(已经尝试过两者,都会产生以下错误):

See this question's first two answers (have tried both, both generate the below error):

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

Code (changed necessary pieces):

$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)

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

Other way (changed necessary pieces):

$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需要验证。
了解更多信息

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

在第一个脚本中,明确指定端口,而不使用PS的内置函数虽然我以前没有这个功能的问题)。

On the first script, the port is explicitly specified, while it's not using PS's built in function (though I haven't had problems with this function in the past).

谢谢!

推荐答案

尝试此功能通过Gmail发送电子邮件:

Try this function to send an email via 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

这篇关于PowerShell发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆