PowerShell-SMTP发送
所以我有一个加密字符串存储在一个文本文件中,我可以反复调用,这是我的SMTP智能主机的密码。这是通过以下方式创建的:PowerShell-SMTP发送,powershell,Powershell,所以我有一个加密字符串存储在一个文本文件中,我可以反复调用,这是我的SMTP智能主机的密码。这是通过以下方式创建的: Read-Host "Enter the Password:" -AsSecureString | ConvertFrom-SecureString | Out-File C:\EncPW.bin 我现在需要以某种方式将它与$username一起传递给发送邮件功能的-Credential参数。我想我需要把它转换成PSCredential格式。但是我被卡住了 $password
Read-Host "Enter the Password:" -AsSecureString | ConvertFrom-SecureString | Out-File C:\EncPW.bin
我现在需要以某种方式将它与$username
一起传递给发送邮件功能的-Credential
参数。我想我需要把它转换成PSCredential格式。但是我被卡住了
$password = cat C:\encPW.bin | ConvertTo-SecureString
$password
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
到目前为止,我有这个,但当我打印它时,它输出了$passwordSystem.Security.SecureString
,我得到一个错误:
net_io_connectionclosed
当它尝试SMTP发送时
干杯
调用SMTP发送:
Send-MailMessage -from "TestPS@mydomain.co.uk" -To "someone@mydomain.co.uk" -Subject "Daily Report" -Body "Open Attachment for Report" -smtpServer 85.119.248.54 -Attachments "C:\WSB_Reports\DailyReport.txt" -Credential $cred
假设您的用户名和密码正确,您采取的方法看起来是正确的。您的用户名是否以域作为前缀,例如domain\username
?我支持通过Get credential
使用手动提供的凭证尝试该命令的建议,例如:
$cred = Get-Credential
如果需要验证从安全字符串返回的密码是否正确,可以按如下方式显示:
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str
无法打印securestring(这是“安全”部分)。你试过什么<代码>发送邮件-凭证$cred
?这就是输出错误的原因吗?是的-然后我得到:发送邮件消息:无法从传输连接读取数据:net\u io\u connectionclosed。您是否尝试在不使用密码bin文件的情况下使用新凭据。只是为了测试一下$creds=Get Credential
是否可以使用脚本运行所在LAN外部的SMTP服务器发送邮件?i、 e.智能主机我很确定,只要你和服务器之间没有防火墙阻止端口25,它就可以。我尝试过使用Get Credential,但出现了相同的错误-请参阅我在上面文章中的评论。谢谢:)请尝试此-telnet mailserver.domain.com 25
,看看您是否可以访问计算机的SMTP端口。我确实可以,ehlo和所有这些。您确定您的凭据授予您使用邮件服务器的权限吗?