PowerShell发送电子邮件

编程入门 行业动态 更新时间:2024-10-11 07:36:04
本文介绍了PowerShell发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

Code (changed necessary pieces):

$EmailFrom = "notifications@somedomain" $EmailTo = "me@earth" $Subject = "Notification from XYZ" $Body = "this is a notification from XYZ Notifications.." $SMTPServer = "smtp.gmail" $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", ("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" $smtpServerport = "587" $emailSmtpUser = "TheBatch@gmail" $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发送电子邮件

本文发布于:2023-11-23 19:17:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!