powershell - 当服务器响应为 5.7.0 时如何修复 'the SMTP server requires a secure connection or the client was not authenticated' 错误?

标签 powershell gmail attachment smtpclient

我确实在 Gmail 中打开了安全性较低的应用程序,我认为问题出在 SSL 上。

My powershell screenshot

My current problem after changing port and $smtp

$time = get-date 
$smtpServer = “smtp.gmail.com”
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential(“user”, “password”)
$smtp = new-object Net.Mail.SmtpClient($smtpServer,587) **587 to 465**
$msg = new-object Net.Mail.MailMessage
$msg.From = (“<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f4e0fcf4f8f0f5d9fef4f8f0f5b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>”)
$msg.To.Add(“<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7aabea2aaa6aeab87a0aaa6aeabe9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>”)
$body=”Your message Body”
$msg.Subject = “Mail From Gmail ” + $time
#your file location
$files=Get-ChildItem “C:\Users\Public\Pictures\LINE”
Foreach($file in $files)
{
Write-Host “Attaching File :- ” $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList 
C:\Users\Public\Pictures\LINE\$file
$msg.Attachments.Add($attachment)
}
$smtp.Send($msg)
$attachment.Dispose()
$msg.Dispose()

最佳答案

您是否尝试过更改端口? 在我看来,您仍在使用 587,它需要 TLS/SSL 或 STARTTLS。

您不进行身份验证和使用 SSL/TLS 是否有原因?

我了解到,如果不支持 STARTTLS,Google 不会回退到纯文本 SMTP。如果您在配置服务器时明确选择了“不安全”选项,我相信您需要使用不同的端口。 25(也许)。

编辑:看起来您缺少发送方法的参数。试试这个代码。 或者尝试更改为看起来更简单的 Send-MailMessage cmdlet https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Send-MailMessage?view=powershell-5.1

$EmailFrom = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e8e9f2efe0efe5e7f2efe9e8f5c6f5e9ebe3e2e9ebe7efe8a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>"
$EmailTo = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea878faa8f8b989e82c4898587" rel="noreferrer noopener nofollow">[email protected]</a>" 
$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)

关于powershell - 当服务器响应为 5.7.0 时如何修复 'the SMTP server requires a secure connection or the client was not authenticated' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55937259/

相关文章:

powershell - 调用批处理文件并等待它完成

android - 是否有任何简单的阅读新 gmail 邮件的简短代码示例?

sql-server - 从作业执行 sp_send_dbmail 失败,查询结果作为文件附加

excel - 发送带附件的电子邮件的 VBA 循环还包括所有以前迭代的附件

Android - 如何找出用户有多少未读电子邮件?

java - 解析电子邮件中签名部分的图像

c# - 使用powershell加载C# DLL,[System.Type]::GetType返回null

regex - 跨多行替换两个单词之间的文件文本

使用 MSVC 从 Windows Powershell 编译 C 程序

php - imap_open() : Couldn't open stream