powershell - 未收到使用 powershell 命令发送的电子邮件

标签 powershell scripting

Send-MailMessage -SmtpServer "smtp.com.au" -From "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0ada580ada5eea3afad" rel="noreferrer noopener nofollow">[email protected]</a>" -To "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e17011b2e17011b400d0103" rel="noreferrer noopener nofollow">[email protected]</a>" -Subject "Testing" -Attachments "\\Test\archive.zip" -Body "attached"

我正在使用上述命令通过电子邮件发送 zip 文件,但我在“收件人”( [email protected] ) 邮箱中没有看到任何内容。

命令成功运行,没有任何错误。

最佳答案

我认为问题出在你的函数调用上。你错过了很多参数。看看我的 Gmail 示例:

$password = ConvertTo-SecureString "123456789" -AsPlainText -Force
$emailCredentials = New-Object pscredential ("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="375145585a77505a565e5b1954585a" rel="noreferrer noopener nofollow">[email protected]</a>", $password)
try {
    Send-MailMessage -From "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0462766b69446369656d682a676b69" rel="noreferrer noopener nofollow">[email protected]</a>" -To "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681c07281c07460b0705" rel="noreferrer noopener nofollow">[email protected]</a>" -Subject "subject" -Body "body" -SmtpServer "smtp.gmail.com" -Port 587 -Credential $emailCredentials -Encoding UTF8 -UseSsl -ErrorAction Stop
}
catch {
    echo $error[0]
}

您的调用中没有 -Credential 参数,我几乎可以肯定,这就是问题所在。另外,您的 smtp 服务器可能在不同端口上有此协议(protocol),这就是 -Port 的用途。最后-UseSsl,如果smtp服务器正在加密邮件,那么如果没有这个开关,您的电子邮件将不会发送。 -编码 UTF8 仅适用于区域字符。

检查您的$error[0],它可能会告诉您问题出在哪里。检查您的 -From 电子邮件发生了什么情况。在 Gmail 上,您有时会看到自动重新编译,并带有错误描述。

很抱歉没有为您提供简单的解决方案,但我没有足够的信息来找到它。

关于powershell - 未收到使用 powershell 命令发送的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200232/

相关文章:

powershell - 如何从Docker传递参数给 “find”?

azure - 启动进程: This command cannot be run due to the error: %1 is not a valid Win32 application on dev azure

powershell - 如何在主代码和后台进程中使用相同的Powershell函数

powershell - $ Event.SourceEventArgs。<attribute>返回一个空或空对象

batch-file - 如何使用命令提示符或批处理文件检查用户是否存在?

powershell - 使用参数在 UNC 路径上运行批处理文件并将输出捕获到变量,无需社区扩展

sql-server - 我可以在 SQL Server 2005 任务中自动返回 'Generate Scripts' 吗?

java - 使用或不使用 Eclipse 生成 Spring SP 类

linux - 如何为给定路径创建文件夹结构和文件

bash - 通过将 dirname 的结果管道化到 Bash 脚本中的 basename 来获取父目录名称