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的内置功能(虽然我以前没有使用过这个功能)。
谢谢!
答案 0 :(得分: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
答案 1 :(得分:0)
尝试此功能通过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